0

I am trying to take a user inputted string and run an if statement that checks if the user inputted string is the desired input.

However, when I run the following program I seem to always get "false" printed even when I input the desired string. I believe it has something to do with the (str == "ABCD") but I am not sure. How can I rewrite this to output "true" when the desired string is input.

Scanner keyboard = new Scanner(System.in);

String str = keyboard.nextLine(); //say the user inputs ABCD

if (str == "ABCD")
{
System.out.print(true);
} else {
System.out.print(false);
}

1 Answers1

0

You can't compare Strings like that in Java.

You can use str.equals() if a string should match exactly
or use str.equalsIgnoreCase() for non-casesensitive checks

Brentspine
  • 274
  • 1
  • 15