• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test user input against an array

 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure if I am going about it the right way but this is what I am trying to do and what I have so far. I want the program to randomly pick something from the terminology array and ask a question for the user to answer. So far I can get it to do that and have the user input the answer but what I need is to check the answer to see if it is right or wrong with what is in the answer array.



Thanks for the help
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you need a statement like


I will leave it to you to figure out where to put that and what to do on right/wrong answers. Also, if the input gets the line termination character, you might need to strip that off before you use it, I can't remember if it does or not.

rc
 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep that was it, knew I needed an if-else statement just wasn't sure how to test it.

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That linkage is exactly what you get from a Map.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Corre wrote:
Off topic, but just a head's-up for you:

In general you should try to avoid constructs like the above, because you're swallowing an exception and replacing it with something that's nowhere near as useful as a stacktrace if, in fact, you do get an error. At least it wasn't an empty catch() block though (a mortal sin) .

You can also avoid the try...catch altogether by simply putting a throws clause on your main() method.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also avoid a try-catch by using a java.util.Scanner. You should use Scanner whenever possible for keyboard input, because it is so much easier to use than a BufferedReader.
 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the info. Been a little while since I have done anything and just trying to get back into things. I had seen the collections but never used them so wasn't sure how to go about that and I am going to replace the reader with the scanner. Now another question for ya, if I wanted to take what I have and run it on my phone would I have to rewrite it in something else? The phone is a DroidX.

Here is the updated code I have.


Thanks again,
Kevin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic