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

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

The two Strings ans and "yes" are equivalent, however they are not equal.

Take a look at the available methods for Strings and see if one of them offers a way to compare Strings by their value

ans is a variable, of type string, so how come it is a reference here?

ans is a reference to a String object, == is not a dependable way to compare String objects whereas string.equals(string) is

But ans was declared as a String; how is it a reference?
Does String ans; mean that ans is declared as a reference to a string (String starts with a capital, so it is a class, right?

Thank you in advance