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

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:

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

PS.
This is my first message here, so I hope I did not break any rule.

If I remember correctly in Java, the:

answer == "yes"

will not work because == does not work in that sense with strings like that. I'm kind of surprised it worked with string numbers (1/0) but meh. Try

"yes".equals(answer)

actually:

while (ans == "yes");

might work in some cases.. but it's not recommended, nor reliable. for Objects, the == operator doesn't compare the values of the two objects, it'll rather compare their references, and, even though the values might be the same for two Objects, that doesn't mean the references are.

Stuugie is right. for the comparison of Objects you should use the .equals method. (or equalsIgnoreCase, for String objects)

anyway, a small demonstration:

String one, two, three;

one = "String";
two = "String";
three = new String("String");

System.out.println("== result: " + (one == two) + " equals result: " + (one.equals(two));
System.out.println("== result: " + (one == three) + " equals result: " + one.equals(three));

Hi

Thank you for the reply.
I changed the while statement to be:

while ( "yes".equals(ans) );

And it works now! So what exactly does "yes".equals() mean!?

I see (almost)
so 'ans' is not a variable but it's an object of the class String, right!? This is why the comparison was not a valid one.

Well, how would one know if the "quantity" he has is variable or an object!? Do I need to know all the classes!?

They are all variables.
Some veriables are "primitives", eg int, float, boolean, char (See Java Language Spec section 4.2 for the complete list, but they alll have names that begin with a lower case letter. All the classes in standard Java have names the begin with an upper-case letter). They just contain a value; and == workslike you would expect.
Others are "reference variables" and they contain a reference ("pointer") to an Object. Thet are not themselves objects, nor do they contain objects, they just have a reference to an object. Their == tests that their references are equal, ie they are both references to the same Object. To see if two different Objects are considered "equal", you have to call their equals method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.