Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make multiple if not cases within the same parens?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java friends!

Wondering here how does one make muliple if not cases within the same parens?

I want it so that if the user types anything but the letters x,y,z,q to run and error message. I was thinking something like:
if(letter != x, y, z, q)
System.out.print("Error");

Do you think this will work? is there an easier way? It doesn't look like I can put multiple letters to not equal inside a single parens.

Here is the original code that I want to put that into:
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String has several methods for matching and locating which you could probably use to when trying to validate against a number of different characters.

For example, if you had a string containing all the valid characters (String validCharacters = "xyzq"), then you could use something like validCharacters.indexOf(letter) to see if the character was one of the ones included in the String.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comparison of Strings is case sensitive in Java.
If you are familiar with regex, you may want to give it a try.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!