Reading a String

I am getting user input via a scanner and am checking what it equals with if (input.equals(“whatever”)), and want to check for multiple things. Example, if an option in the menu is “Attack”, I want to check if they typed “Attack” and “attack” and “a” and many others. Is there a way to do this with only one if statement?

Thanks,
Nathan

if (input.equals("there") || input.equals("is") || input.equals("no") || input.equals("spoon")){
}

If you want to you can also use another version of equals (equalsIgnoreCase or so) if you want to compare both attack and Attack.

You can also go Java7 and do a switch statement :slight_smile:

Mike

if (input.toLowerCase().startsWith("a"))

?

Exactly what I was looking for. I was trying stuff like this but couldn’t get it right.

Thanks!

For case insensitive string checking:


input = input.toLowerCase();
if(.......)