File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java Magic 8 Ball Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Magic 8 Ball" Watch "Java Magic 8 Ball" New topic
Author

Java Magic 8 Ball

Andrew McCarthy
Greenhorn

Joined: Jun 20, 2012
Posts: 22
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.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jun 20, 2012
Posts: 22
Ah, I apologize, I meant Class, not method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jun 20, 2012
Posts: 22
Well, I was able to get it to work. Below is the finished code. I would welcome some comments.

Andrew McCarthy
Greenhorn

Joined: Jun 20, 2012
Posts: 22
Thank you for your help, Jeff.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jun 20, 2012
Posts: 22
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Magic 8 Ball