我有这种方法可以在文本文件中搜索一个词,但它不断给我一个否定的结果,即使这个词在那里?
public static void Option3Method(String dictionary) throws IOException
{
Scanner scan = new Scanner(new File(dictionary));
String s;
int indexfound=-1;
String words[] = new String[500];
String word1 = JOptionPane.showInputDialog("Enter a word to search for");
String word = word1.toLowerCase();
word = word.replaceAll(",", "");
word = word.replaceAll("\\.", "");
word = word.replaceAll("\\?", "");
word = word.replaceAll(" ", "");
while (scan.hasNextLine()) {
s = scan.nextLine();
indexfound = s.indexOf(word);
}
if (indexfound>-1)
{
JOptionPane.showMessageDialog(null, "Word found");
}
else
{
JOptionPane.showMessageDialog(null, "Word not found");
}