• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Test user input against an array

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That linkage is exactly what you get from a Map.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic