This while
loop is strange:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
while (line != null) {
String word = scanner.next();
addWord(word, linecount);
}
linecount++;
}
If your input file is:
Then scanner.nextLine()
would be return a
, then scanner.next()
would return b
, because nextLine
returns the next end-line delimited String, and next
returns the next token from the input file. Is this really what you want? I’d suggest trying this:
while (scanner.hasNextLine()) {{
String word = scanner.nextLine();
addWord(word, linecount);
linecount++;
}
Keep in mind that this would only work if there’s only a word per line. If you want to handle multiple words per line, it’d be slightly longer:
while (scanner.hasNextLine()) {{
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
while(lineScanner.hasNext()) {
addWord(lineScanner.next(), linecount);
}
linecount++;
}