• 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

Java Magic 8 Ball

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to build my first program, and had an idea for a sort of "Magic 8 Ball" thing, where you enter a question and the program outputs a random answer. However, I'm having a great deal of trouble figuring it out. I know I could do it using an array to store the answers and the Math.rand operator to output a random answer, but I can't figure out how to work the Scanner in, or how to make it all work together, and I'm not sure if I can get it to work with just one Class. Please help.

Below is an example of the code I intend to use to generate the answers.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew McCarthy wrote:I can't figure out how to work the Scanner in, or how to make it all work together,



What specific problems are you having with it?

and I'm not sure if I can get it to work with just one method.



Why would you want to? One method should do one thing. For a very rough example here, one method to take user input, one method to get the random answer, one method to output the answer. You might end up breaking those methods down further, or possibly combining a couple of them, but "trying to do it in one method" is not a worthwhile programming goal.
 
Andrew McCarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I apologize, I meant Class, not method.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, well, again, we don't generally try to force something into a single class, although in your case you shouldn't need more than one.

So, if you can show what you've got that works, and then what you tried to do next, and explain exactly what problems you had with that, people here will be happy to help. You need to TellTheDetails(←click) so that we can understand your problem.
 
Andrew McCarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I was able to get it to work. Below is the finished code. I would welcome some comments.

 
Andrew McCarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help, Jeff.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my first thought is "why are you saving their question?" My next thought is "why are you saving their question in a variable called 'name'?"

You don't use it ever, so why bother saving it? and if you are going to save it, a more meaningful variable would be something like userQuestion.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I second Fred's comments. In addtion...

1. wordList never changes, so it can be a static final member variable.

2. wordList is a somewhat ambiguous variable name. I wouldn't include "List", especially since it's an array, not a List. I'd call it "answers" or "responses" or something more descriptive like that.

3. oneLength is a confusing variable name. I'd call it "responseLength" or "numResponses" or something.

4. I'd use java.util.Random, not Math.random(), and I'd make the Random object a static final member variable.
 
Andrew McCarthy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still learning, and haven't learned about some of the things you are talking about, like final, but I thank you for your suggestions.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew McCarthy wrote:I am still learning, and haven't learned about some of the things you are talking about, like final



Yeah, I figured that. Just something to keep in the back of your mind for later.

Good luck!

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic