Results 1 to 3 of 3
  1. #1
    manifest3r is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Getting random word to print out of an Array

    I'm doing this for a class project, hang man, I successfully got it to choose a random word, but it doesn't give me a print out of the word, just the number. When I tell the code to write out the number of the word chosen, it always defaults to null.

    Here's the code:

    Java Code:
    import java.util.Random;
    public class Dictionary{
    
    public static void main(String[] args){
    
    
    
    //Declaring a variable
    //Build an array of words
    {String words[] = new String[23];
     
    words[1] = "dictionary";
    words[2] = "restaurant";
    words[3] = "televiion";
    words[4] = "responsible";
    words[5] = "technology";
    words[6] = "collection";
    words[7] = "communicate";
    words[8] = "automobile";
    words[9] = "federation";
    words[10] = "enterprise";
    words[11] = "exaggerate";
    words[12] = "cappuccino";
    words[13] = "monkey";
    words[14] = "computer";
    words[15] = "information";
    words[16] = "systems";
    words[17] = "poker";
    words[18] = "bottle";
    words[19] = "administration";
    words[20] = "encyclopedia";
    words[21] = "textbook";
    words[22] = "calculus";
    
    for (int index = 0; index<words.length; index++)
        
    System.out.println(words[index]);
    }
    
    //Return a randomly selected word
    {
    String words [] = new String[23];
    
    Random getRandomWord = new Random();
    
    int n = getRandomWord.nextInt(23);
    
    System.out.println("Your random number is " + n);
    System.out.println("Your random word is " + words[n]);
    System.out.println("This word has " + words[n].length() + " characters");
    
    for (int i = 0; i<words[n].length(); i++)
    System.out.print("_ ");
    
        Random rnd= new Random();
        int randomWord=rnd.nextInt(22);
        System.out.println("Your word is   "+ words[randomWord]);
    }
    }
    }

    Here's the part i'm having issues with:
    Java Code:
    System.out.println("Your random number is " + n);
    System.out.println("Your random word is " + words[n]);

    When I try to grab the word, words[n], I get the output for the following line:
    Java Code:
    Your random word is null
    What am I doing wrong?

    Thanks!
    Last edited by manifest3r; 02-16-2011 at 10:50 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You have created 2 String arrays called words. Once has been filled with Strings inside a code block and is not available outside that block. the other is not filled with Strings and therefore is all nulls and is outside the code block. This is the array that you are accessing in the rest of your code.

  3. #3
    manifest3r is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You have created 2 String arrays called words. Once has been filled with Strings inside a code block and is not available outside that block. the other is not filled with Strings and therefore is all nulls and is outside the code block. This is the array that you are accessing in the rest of your code.
    AWESOME! Thanks :)

    I got it to work!

    +rep

Similar Threads

  1. Bug in forum software when last word is print
    By gcalvin in forum Forum Lobby
    Replies: 0
    Last Post: 04-26-2010, 11:44 AM
  2. Replies: 1
    Last Post: 04-15-2010, 05:40 PM
  3. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 09:37 AM
  4. need help print random digital
    By lowpro in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 06:47 AM

Posting Permissions

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