Results 1 to 10 of 10
- 03-10-2010, 01:23 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
How do i return a random String from an array?
Hi all, i have this assignment to make a hangman sort of game. So far i've made a class which is required to hold possible words for the player to guess in an array. Also there should be some way for the Strings to be automatically loaded into the array when the program is started. the class should contain a single method to randomly select and return a word from the array for the player to guess. So all this should be doable in a single class.
Im fairly new to programming and use the IED BlueJ.
Im stuck on trying to select and return a random word frrom my array, heres what i have so far:
import java.util.Random;
/**
* A class to hold a list of target words.
* @author .
* @version 2010.03.10
*/
public class Database
{
public String anArrayOfTargetWords[];
Random generator = new Random();
public void setUpTargetWords() {
String[] anArrayOfTargetWords; // declares an array of Strings
anArrayOfTargetWords = new String[5]; // allocates memory for 5 Strings
anArrayOfTargetWords[0] = "red"; // initialize first element
anArrayOfTargetWords[1] = "blue"; // initialize second element
anArrayOfTargetWords[2] = "green"; // etc.
anArrayOfTargetWords[3] = "purple";
anArrayOfTargetWords[4] = "orange";
}
/**
* randomly select string
*/
public static String get (String[] array) {
int rnd = generator.nextInt(array.length);
return array[rnd];
}
This gives me an error with the method i have saying that i cannot reference a non-static variable 'generator' from a static context.
Any help would be much appreciated as as soon as possible, thanks in advance.Last edited by Grendel0; 03-10-2010 at 01:25 PM.
- 03-10-2010, 03:41 PM #2
- 03-11-2010, 08:19 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
Thanks for the reply, unfortanately my assignment requires me to use BlueJ so i gotta live with it. So any ideas what to change the method to? im so bad at programming and bluej des not help.
- 03-11-2010, 08:40 AM #4
Remove the static from the method declaration.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-11-2010, 09:24 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
thanks i wored that out lol, now i get no errors when compiling, however when i run the getWord() method i get a error :(. heres my code and error message;
public class Database
{
Random generator = new Random();
public String anArrayOfTargetWords[];
/**
* Constructor for objects of class Database
*/
public Database()
{
String anArrayOfTargetWords[]; // declares an array of Strings
anArrayOfTargetWords = new String[5]; // allocates memory for 5 Strings
anArrayOfTargetWords[0] = "red"; // initialize first element
anArrayOfTargetWords[1] = "blue"; // initialize second element
anArrayOfTargetWords[2] = "green"; // etc.
anArrayOfTargetWords[3] = "purple";
anArrayOfTargetWords[4] = "orange";
}
/**
*
*/
public String getWord() {
int n = generator.nextInt(anArrayOfTargetWords.length);
return anArrayOfTargetWords[n];
}
}
ERROR:
java.lang.NullPointerException
at Database.getWord(Database.java:31)
this is in the line "int n = generator.nextInt(anArrayOfTargetWords.length);"
Please help
- 03-11-2010, 09:36 AM #6
Your instance array anArrayOfTargetWords is null. You've declared a method(constructor) local anArrayOfTargetWords that get's initialized.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-11-2010, 09:41 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
thanks i gathered that, how do i fix it?
- 03-11-2010, 09:48 AM #8
Remove the constructor local array.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-11-2010, 09:51 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 11
- Rep Power
- 0
Cool thankyou so much :D
- 03-11-2010, 10:11 AM #10
You're welcome.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
Static String Return Type
By Java_Developer in forum New To JavaReplies: 17Last Post: 10-03-2009, 06:07 PM -
how do i make a string return a number?
By pjr5043 in forum New To JavaReplies: 6Last Post: 09-15-2008, 04:56 AM -
[SOLVED] Get a random String
By LeoCoderIV in forum New To JavaReplies: 6Last Post: 04-07-2008, 02:58 PM -
random string are duplicate
By googgoo in forum New To JavaReplies: 3Last Post: 04-03-2008, 10:01 AM -
HashMap String Return
By kizilbas1 in forum New To JavaReplies: 1Last Post: 03-10-2008, 03:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks