In Java, I'm working on a program that reads a given text file and records words for the number of times they appear, and every spot in which they appear (in the format "lineNumber, wordNumber").
Though my methods for using the information are solid, I'm having trouble coming up with an algorithm that properly counts both the lines and the placements (beyond the words in the first line).
For example, if the text is
hello there
who are you hello
The word objects would be given the information
hello appearances: 2 [1-1] [2-4]
there appearances: 1 [1-2]
who appearances: 1 [2-1]
are appearances: 1 [2-2]
you appearances: 1 [2-3]
Here's a basic version of what I have:
lineNumber = 0;
wordNumber = 0;
while (inputFile.hasNextLine())
{
lineNumber++;
while (inputFile.hasNext())
{
wordNumber++;
word = inputFile.next();
//an algorithm to remove cases that aren't letters goes here
Word w = new Word(word);
w.setAppearance(lineNumber, wordNumber);
}
But of course the problem with this approach is that the hasNext()
conflicts with the hasNextLine()
since HasNext()
apparently goes to the next line in the text file automatically, so lineNumber doesn't get a chance to increment, so any word after line 1 gets incorrect recordings.
How could I fix this? If this is complex enough that I'd need another import, what should I use?