Browse Source

changed style

master
school 7 years ago
parent
commit
5c1d98c95d
  1. 21
      Dictionary.java

21
Dictionary.java

@ -1,38 +1,31 @@
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
public class Dictionary
{
public class Dictionary {
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
private ArrayList<String> wordList; private ArrayList<String> wordList;
public Dictionary()
{
public Dictionary() {
loadWords(); loadWords();
} }
public String getRandomWord()
{
public String getRandomWord() {
int idx = (int)(Math.random() * this.wordList.size()); int idx = (int)(Math.random() * this.wordList.size());
return wordList.get(idx); return wordList.get(idx);
} }
private void loadWords()
{
private void loadWords() {
this.wordList = new ArrayList<String>(); this.wordList = new ArrayList<String>();
File f = new File("words.txt"); File f = new File("words.txt");
try
{
try {
Scanner in = new Scanner(f); Scanner in = new Scanner(f);
while(in.hasNext())
{
while (in.hasNext()) {
String word = in.next(); String word = in.next();
wordList.add(word); wordList.add(word);
} }
in.close(); in.close();
} }
catch(FileNotFoundException ex)
{
catch (FileNotFoundException ex) {
System.out.println("File not found."); System.out.println("File not found.");
} }
} }
Loading…
Cancel
Save