import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
/**
* Opens dictionary file and reads the lines, putting them into array
* Removes invalid lines (non alphabetical)
* Selects a random word, and keeps count of words used
*
* @author: Leon Traill
* @version: 0.01
*/
public class Dictionary
{
private Random randomGenerator;
private ArrayList<String> words;
int wordCount;
// Allow different file names to be used
public Dictionary(String fileName)
{
randomGenerator = new Random();
words = new ArrayList<String>();
try
{
// Open the file
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
int index = 0;
// Read each line and add into array
while( line != null )
{
words.add(line);
index++;
line = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
/**
* Remove lines which contain characters not from the alphabet (A-Z, a-z)
*/
public void cleanList()
{
// Check array, remove all non-letter characters
for( int index = 0; index < words.size(); index++ )
{
if( index < words.size() && (!words.get(index).matches("[A-Za-z]+")) )
{
words.remove(index);
// Decrement so the loop does not miss entries when removing
index--;
}
}
}
/**
* Accessor for currentWord
*
* @return
*/
public String chooseNewWord()
{
String currentWord;
int index = randomGenerator.nextInt(words.size());
currentWord = words.get(index);
wordCount++;
return currentWord;
}
}