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));
}
}