I
I
itfan2020-10-01 12:16:29
Java
itfan, 2020-10-01 12:16:29

Why doesn't if process the results from the scanner?

Scanner inData = new Scanner(System.in);
System.out.println("Would you like to continue?");
String answer = inData.nextLine();
if (answer == "yes"){
System.out.println("Test!");
}


I can't understand at all. If you just display it on the screen, it displays yes without problems. If you hardcode answer and then compare, it works correctly. However, if you take inData.next() or inData.nextLine(), the comparison does not work for some reason. Test is only output if != yes. What's the catch?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question