> Why doesn't this Java code work? Was trying to break out of a while loop using a break statement in my conditional..

Why doesn't this Java code work? Was trying to break out of a while loop using a break statement in my conditional..

Posted at: 2014-12-18 
When comparing string "==" compares reference equality and ".equals()" compares value equality.

So you should rewrite if(choice.toLowerCase() == "pass") break; as if(choice.toLowerCase().equals("pass")) break;

Ah, you're right! Thanks a bunch.

Here is the code:

import java.util.Scanner;

public class Test

{

public static void main(String[] args)

{

String choice = "default";

Scanner scan = new Scanner(System.in);



while(choice.toLowerCase() != "pass")

{

System.out.println("What do you do: ");

choice = scan.nextLine();



if(choice.toLowerCase() == "pass") break;

}



System.out.println("You broke out of the loop.");

}

}