You're main problem is here...
FileReader Search = new FileReader(f);
Scanner in = new Scanner(f);
LineNumberReader L = new LineNumberReader(Search, x);
StreamTokenizer token = new StreamTokenizer(Search);
while (in.hasNextLine())
{
You've basically opened two file readers against the same file, but you seem to be expecting them to know about each other. You advance the Scanner
, but that has no effect on the LineNumberReader
. This then messes up the reporting and line reading process.
Reading from Scanner
should look more like...
while (in.hasNextLine()) {
String text = in.nextLine();
Having said that, I'd actually drop the Scanner
in favor of the LineNumberReader
as it will provide you with more useful information which you would otherwise have to do yourself.
For example...
FileReader Search = new FileReader(new File("TestFile"));
LineNumberReader L = new LineNumberReader(Search, x);
String text = null;
while ((text = L.readLine()) != null) {
// Convert the two values to lower case for comparison...
if (text.toLowerCase().contains(input.toLowerCase())) {
System.out.println(L.getLineNumber() + ": " + text);
}
}