Friday 1 April 2016

Java Scanner Class

I am writing a program that should close the console if the user input the String "end'.
The program always performs the else loop even if the user inputs "end". I'm wondering why the program is not getting into the if part of the loop and shutting down.



Scanner scan = new Scanner(System.in);
while(true)
{
String num = scan.nextLine();


if(num == "end")
{
System.exit(0);
}
else
{
System.out.println("hi");
}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...