Here is my code:
// Import io so we can use file objects
import java.io.*;
public class SearchThe {
public static void main(String args[]) {
try
Use Regexes case insensitively, with word boundaries to find all instances and variations of "the".
indexOf("the") can not discern between "the" and "then" since each starts with "the". Likewise, "the" is found in the middle of "anathema".
To avoid this, use regexes, and search for "the", with word boundaries (\b) on either side. Use word boundaries, instead of splitting on " ", or using just indexOf(" the ") (spaces on either side) which would not find "the." and other instances next to punctuation. You can also do your search case insensitively to find "The" as well.
Pattern p = Pattern.compile("\\bthe\\b", Pattern.CASE_INSENSITIVE);
while ( (line = bf.readLine()) != null) {
linecount++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while (m.find()) {
System.out.println("Word was found at position " +
m.start() + " on line " + linecount);
}
}