Hello.
You may be making one more mistake in your code.
In the beginning when you are using scanner for file, you are checking for next word using scanner.hasNext() but reading the entire line....