I see absolutely no problem with the code, I tried it with the following test file:
word line
number two
alice and bob
bar bazz
loop for each
buzz bizz bar bozz
this is some text
lorem ipsum bar
buzz isobar
And searched for "bar":
System.out.println(Search("bar"));
And I got the following (expected) result:
4: bar bazz
6: buzz bizz bar bozz
8: lorem ipsum bar
So, it's correctly identifying the lines containing the word (it even skips the last line containing "bar" as part of another word).
My best guess is that you are passing a wrong file path, so you have a FileNotFoundException
, but as you are ignoring it, you have no stack trace, and nothing to help you. Try to print the exception in the catch clause, and re-run you program:
catch(FileNotFoundException e)
{
e.printStackTrace();
}
My second best guess is that your test case is bad (like if the string you are searching is part of another word, or if you have some punctuation just after the word). Try to run my test case, and see if it works.