You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
799 B

7 years ago
7 years ago
  1. import java.util.*;
  2. import java.io.*;
  3. public class Dictionary
  4. {
  5. public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
  6. private ArrayList<String> wordList;
  7. public Dictionary()
  8. {
  9. loadWords();
  10. }
  11. public String getRandomWord()
  12. {
  13. int idx = (int)(Math.random() * this.wordList.size());
  14. return wordList.get(idx);
  15. }
  16. private void loadWords()
  17. {
  18. this.wordList = new ArrayList<String>();
  19. File f = new File("words.txt");
  20. try
  21. {
  22. Scanner in = new Scanner(f);
  23. while (in.hasNext())
  24. {
  25. String word = in.next();
  26. wordList.add(word);
  27. }
  28. in.close();
  29. }
  30. catch (FileNotFoundException ex)
  31. {
  32. System.out.println("File not found.");
  33. }
  34. }
  35. }