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.

31 lines
778 B

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