1

I am not a professional programer. I am still learning, so my code is a little basic right now.

Scanner UserInput = new Scanner(System.in);
String UserChoose = UserInput.next();
if (UserChoose=="Quit"){

I have deduced that there is something missing in the if statement, but I cannot figure out what. Can someone please tell me what I am missing? I have been searching online for an hour with no luck.

3 Answers3

0
if (UserChoose.equals("Quit")){

In java, the default == operator compare if they both are the same object, even if the content is the same, if the object reference is not the same, it isn't ==.

See this link for more complete explanation: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

Justin Lessard
  • 10,804
  • 5
  • 49
  • 61
0

To compare objects in java use .equals() method instead of "==" operator

Replace if (UserChoose=="Quit"){ with if (UserChoose.equals("Quit")){

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

Common mistake, use UserChoose.equals("Quit") to compare strings. Since String is an object, using == will likely compare the memory location the 2 strings or something that would always result with false.

ThaBomb
  • 702
  • 6
  • 11