-1
 package hangman;

 import java.util.Random;

 public class Dictionary {

     int NUMBER_OF_WORDS = 81452;
     public static String FILE_NAME = "dictionaryCleaned.txt";
     private String[] dictionary = new String[NUMBER_OF_WORDS];

     public string getRandomWord() {
          Random rand = new Random();

          return randWord;
     }

 }

I am trying randomly generate a word from the array that contains the text file "dictionaryCleaned.txt" so that I can return the random word to my main method in a separate class. I feel like there could be something wrong with the array, as it does not appear to have the text file loaded into the empty array spaces. I need to return a word from "dictionary", but it is eluding me.

Thank you

rogcg
  • 10,451
  • 20
  • 91
  • 133
  • 3
    is there a code where you load the contents of the text file into the array or you wait it to happen magically? – Nuri Tasdemir Aug 27 '14 at 14:41
  • 1
    Check for a "File to String Array" topic first. Then check for a "Java Random" tutorial. Then edit your question. Then ask us to write comments or answers again. – Seyf Aug 27 '14 at 14:41
  • You haven't popluated the dictionary with any data. http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java getRandomWord() should return a String not string. The correct way to use random is answered below. – Revive Aug 27 '14 at 14:47

5 Answers5

2

Assuming you left out the file reading part,

I would keep the Random() as instance variable in your Dictionary here.

 private final Random random = new Random();

And:

 public String getRandomWord() {
      return dictionary[random.nextInt(dictionary.length)];
 }
Arve
  • 8,058
  • 2
  • 22
  • 25
0

Returning a random word from your dictionnary is easy:

public String getRandomWord() {
     final Random rand = new Random();

     return dictionary[rand.nextInt(dictionary.length)];
}

But you still have to write the code to load your file in your dictionnary.

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
0

considering your dictionary reference has all the data loaded from the dictionary file, then to load random string you can use

String randomValue = dictionary[new Random().nextInt(dictionary.length)];
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
0

Your getRandomWord method should be:

public String getRandomWord() {
   Random rand = new Random();
   int index = rand.nextInt(dictionary.length);
   String randWord= dictionary[index];

   return randWord;
 }
Omoro
  • 972
  • 11
  • 22
  • The person who down voted this, will you mind explaining, that will help me as well as others. – Omoro Aug 27 '14 at 14:54
0

First you must load your words to the array. Then only you can get a random word from your word array.

so your code should be like package hangman;

import java.util.Random;

public class Dictionary {
     int NUMBER_OF_WORDS = 81452;
     public static String FILE_NAME = "dictionaryCleaned.txt";
     private String[] dictionary = new String[NUMBER_OF_WORDS];

     public void loadWordList(String fileName){
          // write your logic to read the file and load them into the dictionary array
     }

     public String getRandomWord() {
          Random rand = new Random();
          return dictionary[rand.nextInt(NUMBER_OF_WORDS)];
     }
 }