Post: help with easy java program
10-23-2011, 07:45 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); hey guys for my class i need to write a java program to reenact a magic 8 ball, so you type in a text field, and click a button, and it will randomly pick an answer from a list. im just having a hard time getting this to work i have been trying to use the RandomOneOf() function but i cant seem to get it to work
(adsbygoogle = window.adsbygoogle || []).push({});

The following user thanked j_lindstrom for this useful post:

Docko412
10-23-2011, 10:05 PM #2
well we're not going to just right up a program for you, yeah we can, but no we're not going to that's not what this sites for, if you need help with the coding, you can post your codes here, and we'll gladly help you check syntax errors etc. but as for saying hey i need this, and waiting this is probably the most logical reply you'll get.
10-23-2011, 10:56 PM #3
Epic?
Awe-Inspiring
Originally posted by lindstrom View Post
hey guys for my class i need to write a java program to reenact a magic 8 ball, so you type in a text field, and click a button, and it will randomly pick an answer from a list. im just having a hard time getting this to work i have been trying to use the RandomOneOf() function but i cant seem to get it to work


Normally I would be like D3ss3rtPr0ducts and give you a similar answer, but I'm actually feeling pretty nice (its a sunny day, I just ate a slice of cake, so I'm pretty happy) and this is an extremely easy answer...


So, you're taking a class of some sort, and they've asked you to write a program in Java that can act like a "magic 8 ball" by randomly picking an answer from a list of possible answers.

First off, you really should tell us where you're at so far by actually posting your code, believe it or not, that's the most efficient way for us to help you, plus, it provides me with some context, because right now I don't know what you're using to store this "list", and there's a variety of other unknowns.

Here's a basic implementation:
    
import java.util.Random; // import the random class

private static string pickRandomString(String[] words) // create a method called "pickRandomString" (takes a String array called "words")
{
Random rand = new Random(); // create a new instance of the random class, this will be used to generate a random integer

return words[rand.nextInt(words.length)]; // returns the value of the next integer from the random number generator (the integer will be between 0 and the length of the words array)
}


That example uses the Random class to generate a random integer, which we use to select a random value from an array.

If you have to use Lists (not arrays) you should be able to easily modify what I have above to something that works for you.

The following 2 users say thank you to Epic? for this useful post:

Docko412, Pichu
10-24-2011, 12:42 PM #4
X0Reax
Keeper
Might be better to just do your homework and learn Java yourself?

The following user thanked X0Reax for this useful post:

Docko412
10-28-2011, 12:02 AM #5
Pichu
RIP PICHU.
Originally posted by Epic
Normally I would be like D3ss3rtPr0ducts and give you a similar answer, but I'm actually feeling pretty nice (its a sunny day, I just ate a slice of cake, so I'm pretty happy) and this is an extremely easy answer...


So, you're taking a class of some sort, and they've asked you to write a program in Java that can act like a "magic 8 ball" by randomly picking an answer from a list of possible answers.

First off, you really should tell us where you're at so far by actually posting your code, believe it or not, that's the most efficient way for us to help you, plus, it provides me with some context, because right now I don't know what you're using to store this "list", and there's a variety of other unknowns.

Here's a basic implementation:
    
import java.util.Random; // import the random class

private static string pickRandomString(String[] words) // create a method called "pickRandomString" (takes a String array called "words")
{
Random rand = new Random(); // create a new instance of the random class, this will be used to generate a random integer

return words[rand.nextInt(words.length)]; // returns the value of the next integer from the random number generator (the integer will be between 0 and the length of the words array)
}


That example uses the Random class to generate a random integer, which we use to select a random value from an array.

If you have to use Lists (not arrays) you should be able to easily modify what I have above to something that works for you.


Epic is one epic son of a bitch... now make me a sandwich.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo