For a mini game in where the user tries to guess a name. But when I want to compare two text strings just to check if are the same it seems that doesn't work.
final String miName = "Jhon";
Scanner input = new Scanner(System.in);
System.out.println("Guess my name: ");
while (true) {
String attempt = input.next();
if(attempt == miName) {
System.out.println("Congrats!");
break;
} else {
System.out.println("Try it again!");
}
}
The output:
Guess my name::
Karl
Try it again!
jhon
Try it again!
Jhon
Try it again!