0

I'm having an issue initiating a random word from the array. I'm not sure how to refer to the words arraylist to fetch from it. Can someone put me in the right direction for my getRandomWord class? Thanks!

A method getRandomWord which takes nothing as input and returns a random String from words. Remember that you can use the Random class to do this.

import java.io.*; 
import java.util.*;
import java.util.Random;

public class WordList{

  private ArrayList<String> words;

  //Construct String from file
  public static void constructor(String filename) throws IOException{

  ArrayList words = new ArrayList();
  BufferedReader read = new BufferedReader(new FileReader("filename"));
  String line = read.readLine();


    while (line != null){
      words.add(line);
      //line = reader.readline();
    }
  }

  public static void getRandomWord(){
Random rand = new Random();
String randomWord = words.get(rand.nextInt(words.size));
}
}
user3062703
  • 1
  • 1
  • 1
  • 1
  • So you're trying to get a random word from a file and output it? – answerSeeker Dec 03 '13 at 18:49
  • Member variables (e.g. 'words') cannot be referenced from static methods (e.g. 'getRandomWord()'). Either declare 'words' as static or remove 'static' keyword from method signature. – Hollis Waite Dec 03 '13 at 18:49
  • you should use something like : int rand= minimum + (int)(Math.random()*maximum); – Clad Clad Dec 03 '13 at 18:50
  • Try this link maybe it will help you [here](http://stackoverflow.com/questions/19125530/how-to-choose-a-random-element-in-this-array-only-once-accros-all-declared-objec) – answerSeeker Dec 03 '13 at 18:52
  • @TatakaiWasumi Indeed. From what I understand the first part of my code takes words from a file and puts them in an arraylist. The second part of my code needs to generate a random number and from that, output the corresponding word in the arraylist. I'm having issues with the fact that words is private... Says I can't reach it from getRandomWord – user3062703 Dec 03 '13 at 18:57
  • Ok, thanks for the clearing that out – answerSeeker Dec 03 '13 at 19:10
  • what happens when you make your array list static? `public static ArrayList words;` ` – answerSeeker Dec 03 '13 at 19:11
  • Your method lacks a return statement. And the return type; it should be `String` instead of `void`. – Holger Dec 03 '13 at 19:34
  • @TatakaiWasumi Thank you, that helped! I'm getting closer... Now the error I get has to do with the size of arraylist words:"Size has private access in java.util.arraylist"in my getRandomWord class. – user3062703 Dec 03 '13 at 19:47
  • Did you already figure it out? if not, what do u have in main()? – answerSeeker Dec 03 '13 at 23:31
  • @user3062703: Use the [`size()`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#size()) method, not the private field. – Holger Dec 04 '13 at 13:58

1 Answers1

3

This will help you I think It's able to get a random word from an array of strings

private static String[] names = { "Terminator", "Slicer","Ninja", "cow", "Robot", "littlegirl" };
name = names[(int) (Math.random() * names.length)];
System.out.println(name);
answerSeeker
  • 2,692
  • 4
  • 38
  • 76