You are not comparing strings correctly. You should use the equals() method, for example:
if (answer.equals("yes"))
When you program in Java, the == operator is usually used to compare primitive data types ( int , double , etc.). If you use == to compare two types of objects (for example, strings), you compare them for identification, that is, you check to see if they refer to one object in memory. In your case, you need to compare if they are equal: if they have the same value (a string of characters in this case), even if they are two different objects, and for this you should use equals() .
EDIT:
Even better, to prevent a NullPointerException , it is considered good practice to reverse the comparison order and first write the line with which you are comparing, for example:
if ("yes".equals(answer))
The explanation is simple: if for some reason the answer is null , the above comparison will be evaluated to false (which means: answer not "yes" ), while the first version of the code will call a NullPointerException when trying to call the equals() method for null .
source share