Sponsored Content
Full Discussion: Help with a Java code
Top Forums Programming Help with a Java code Post 302884285 by faizlo on Sunday 19th of January 2014 10:16:28 PM
Old 01-19-2014
Help with a Java code

Hi all

I am currently learning Java (and Fortran!) on my own. I have written the following code but for some reason it does not work as it should
Code:
import javax.swing.JOptionPane;
class evenOdd {
    public static void main(String[] args) {
        String number, ans;
        int num;
        do {
        number = JOptionPane.showInputDialog(null, "Enter a number (int)", "Even or Odd?", JOptionPane.QUESTION_MESSAGE);
        num = Integer.parseInt(number);
        if (num%2 == 1)
            JOptionPane.showMessageDialog(null, "The number is Odd", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(null, "The number is even", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
        ans = JOptionPane.showInputDialog(null, "Do you want to try again? (yes/no)", "Try Again?", JOptionPane.QUESTION_MESSAGE);
        } while (ans == "yes");
    }
}

I have changed the code a little so the user will answer the last question via numbers (1/0) and it worked as it should. Here is the changed version of the code:
Code:
import javax.swing.JOptionPane;
class evenOdd {
    public static void main(String[] args) {
        String number, answer;
        int num, ans;
        do {
            number = JOptionPane.showInputDialog(null, "Enter a number (int)", "Even or Odd?", JOptionPane.QUESTION_MESSAGE);
        num = Integer.parseInt(number);

        if (num%2 == 1)
            JOptionPane.showMessageDialog(null, "The number is Odd", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(null, "The number is even", "Even or Odd!", JOptionPane.INFORMATION_MESSAGE);

        answer = JOptionPane.showInputDialog(null, "Do you want to try again? (1/0)", "Try Again?", JOptionPane.QUESTION_MESSAGE);
        ans = Integer.parseInt(answer);
        } while (ans == 1);
    }
}

I will appreciate any help to understand this issue.

~faizlo
 

10 More Discussions You Might Find Interesting

1. Programming

Help with Java code

public class MyMail { public static void main(String args) { // TODO Auto-generated method stub System.out.println("number of args " + args.length); String from = ""; String to = ""; String subject = ""; String emailText = ""; for... (0 Replies)
Discussion started by: eel
0 Replies

2. Programming

Help with PrintWriter code in JAVA

I'm creating a file which contained the field name, student ID, house phone number, mobile number and address. Sometimes people don't enter the house phone number. However, in the file I created still print out the house phone number without any data. How do I get rid of this field when people... (1 Reply)
Discussion started by: eel
1 Replies

3. Programming

Help with splitter code in JAVA

I was creating a file using splitter and printwriter. The result in the file come out as: TO:bbb,ccc,eee Instead of, TO:bbb TO:ccc TO:eee May I know what's wrong with this? (1 Reply)
Discussion started by: eel
1 Replies

4. Programming

can i have an optimal solution for this java code ? Facing Java heap space problem even at 3GB heaps

My desired output is run: for this 1 for this 2 for this 3 for this 4 for this 5 for this 1,2 1->2 for this 2,3 2->3 for this 3,4 3->4 for this 4,5 4->5 for this 1,2,3 1->2,3 (2 Replies)
Discussion started by: vaibhavkorde
2 Replies

5. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

6. Programming

Script Shell in java code

Hello, This is my script shell: echo Mon premier script echo Liste des fichiers : ls -la exit 0 This is my code java: public class test { public static void main(String args) { try { Process process = Runtime.getRuntime().exec("sh script1.sh"); } catch... (2 Replies)
Discussion started by: chercheur857
2 Replies

7. Shell Programming and Scripting

Code java in script shell

Hello; Is it possible to insert Java code in a shell script, if so how please? Thank you (0 Replies)
Discussion started by: chercheur857
0 Replies

8. UNIX for Dummies Questions & Answers

Script Shell in java code

Hello, I try to run a script shell from a java program: but it runs only if i do :chmod 777 myShellScript in the terminal Please how can i insert chmod 777 in my java code without going through the terminal? Thank you (1 Reply)
Discussion started by: chercheur857
1 Replies

9. Linux

Linux through Java code

Hi How can I write a Java program to execute Linux commands? What is the best approach: Invoking the Linux shell within Java and executing commands or, using the Java APIs to do the stuff. Since it is platform independent, it'll know itself what to do. We need not check which OS the Java... (1 Reply)
Discussion started by: Dorothy
1 Replies

10. Programming

Java static code

HI All I am new to java in below code I am not able to understand the below line of code Student9.change(); the complete code is mentioned as below class Student9{ int rollno; String name; static String college = "ITS"; static void change(){ college = "BBDIT"; ... (2 Replies)
Discussion started by: scriptor
2 Replies
jconsole(1)						      General Commands Manual						       jconsole(1)

NAME
jconsole - J2SE Monitoring and Management Console SYNOPSIS
jconsole [ options ] [ pid | [ host:port ... ] ] PARAMETERS
pid Process ID of a Java VM that was started with -Dcom.sun.management.jmxremote option on the same machine. See JMX Monitoring and Management for details at http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html. host:port Host name on which the Java VM is running and the port number through which the Java VM's JMX Connector is published. See JMX Monitoring and Management for details at http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html. DESCRIPTION
The jconsole command launches a graphical console tool that enables you to monitor and manage Java applications and virtual machines on a local or remote machine. NOTE- This tool is experimental and may not be available in future versions of the JDK. OPTIONS
-interval=n Set the update interval to n seconds (default is 4 seconds) -notile Do not tile windows initially (for two or more connections) -version Output version information abd exit. -help Output help message and exit. SEE ALSO
Monitoring and Management for Java Platform at http://java.sun.com/j2se/1.5.0/docs/guide/management/index.html 06 Feb 2004 jconsole(1)
All times are GMT -4. The time now is 03:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy