I wrote a program to accept input from a text file, and display the words from the file in ascending order with no duplicates. The output is correct if there is no exception thrown. If the exception is thrown, the user is asked for valid input and the initial method is repeated. When this happens, and a valid input is finally entered, the output is duplicated.
I know something is not being reset, but I cannot figure out what it is.
public void go() {
getWords();
System.out.println(wordList);
wordList = new ArrayList<String>(new HashSet<String>(wordList));
Collections.sort(wordList);
System.out.println(wordList);
}
void getWords() {
try {
File file = new File(getInput());
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
addWord(line);
}
} catch(Exception ex) {
System.out.println("Invalid file name, try again.");
go();
}
}
void addWord(String lineToParse) {
String[] tokens = lineToParse.split("\\s");
for(int i = 0; i < tokens.length; i++) {
wordList.add(tokens[i]);
}
}