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

recognize a user input as one of many strings.  RSS feed

 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the user input to be one of those 3 strings... which i defined earlier...

The problem now is when the user have to put a characters name....
Its alot of strings... about 100...
how can i check if its one of the strings i defined earlier?
I could do a huge if code....
but Im sure I just dont have enought knowlage. any simple solutions?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add all of your recognized values to a Collection of some sort (like an ArrayList), then you can use its contains() method to see if the user input is one of them.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a note that if speed is an issue (and I don't think it is here) then you can use a HashMap. Put the strings into the keys and the values can be anything or nothing. Then use containsKey(). This makes use of the hash searching algorithm in Map which is much faster than the linear search in List.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stuff i havnt learn yet...
Google here i come..

Thanks
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't be afraid to post back here if you get into trouble. Good luck!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!