This question already has an answer here:
- How do I compare strings in Java? 23 answers
I am creating a program in Java, and here is part of the code:
System.out.println("Do you want to leave (y/n)? ");
leave = new Scanner(System.in);
String answer = leave.nextLine().toLowerCase();
if (answer == "n") {
restart();
}
but, when I run this, and type in 'n', it doesn't restart. Is it something to do with the Scanner adding chars to the end of the line?
-Thanks
Aucun commentaire:
Enregistrer un commentaire