mercredi 1 juillet 2015

Randomly choose a word from a text document

I have a method called getWord() and I don't know what to add to it to actually choose a word from a text file. My text file consists of 5 words. Its easy printing all words in document, but how can I print one word differently each time I run the program. My code is below.

private Scanner file;
private final List<String> words;

public TextFile(){
    words = readFile();
}

public String getWord(){

return numOfWords;

}

private List<String> readFile() {

    List<String> wordList = new ArrayList<String>();

    try {
        file = new Scanner(new File("words.txt"));

        }

    } catch (FileNotFoundException e) {
        System.out.println("File Not Found");
    } catch (Exception e) {
        System.out.println("IOEXCEPTION");
    }

    return wordList;
}

public static void main(String[] args) {

    TextFile file = new TextFile();
}

Aucun commentaire:

Enregistrer un commentaire