Results 1 to 10 of 10
  1. #1
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Unhappy 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 02:25 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by Grendel0 View Post

    Im fairly new to programming and use the IED BlueJ.
    Very bad. Don't use an IDE if you're new.

    Quote Originally Posted by Grendel0 View Post
    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.
    The static context is the static method, a class method, without an instance. But without an instance you don't have a Random object. Either you change the method or the Random object definition.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    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.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Remove the static from the method declaration.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    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

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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

  7. #7
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    thanks i gathered that, how do i fix it?

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Remove the constructor local array.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    Grendel0 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    Cool thankyou so much :D

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

Similar Threads

  1. Static String Return Type
    By Java_Developer in forum New To Java
    Replies: 17
    Last Post: 10-03-2009, 07:07 PM
  2. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 05:56 AM
  3. [SOLVED] Get a random String
    By LeoCoderIV in forum New To Java
    Replies: 6
    Last Post: 04-07-2008, 03:58 PM
  4. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 11:01 AM
  5. HashMap String Return
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 04:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •