mercredi 20 juillet 2016

If statement not working correctly Java [duplicate]

This question already has an answer here:

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