0

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]);
    }
}
snaper
  • 5
  • 3

2 Answers2

1

When you call go() from the catch block, you're forgetting that the original go() invocation is still on the stack. It doesn't disappear just because you've called the method again. Essentially, you're performing some accidental recursion.

So in this case, you'll call go() a second time, it will execute as per normal, then the stack will "unwind" and execute the remainder of the first invocation of the go method, printing out the wordlist, sorting it, then printing it out again.

A quick fix might be to have getWords() return a boolean (false if an exception is thrown), then just change the first line to while(!getWords());.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

It's because you are calling go, method inside the catch block which cause printing of the output again, you could correct it like this,

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.");
        getwords(); //Changed
    }
}

void addWord(String lineToParse) {
    String[] tokens = lineToParse.split("\\s");
    for(int i = 0; i < tokens.length; i++) {
        wordList.add(tokens[i]);
    }
}
Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • Thank you, I've been staring at this for a long time and knew it was something simple I was overlooking. – snaper Jul 22 '14 at 01:54