aspose file tools*
The moose likes Java in General and the fly likes Test user input against an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Test user input against an array" Watch "Test user input against an array" New topic
Author

Test user input against an array

Kevin Corre
Greenhorn

Joined: Oct 26, 2011
Posts: 18
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

Joined: May 29, 2005
Posts: 479
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

Joined: Oct 26, 2011
Posts: 18
Yep that was it, knew I needed an if-else statement just wasn't sure how to test it.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
That linkage is exactly what you get from a Map.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Oct 26, 2011
Posts: 18
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Test user input against an array