我正在编写一种方法来搜索列表形式的单词文本文件,对于用户输入的单词,但如果找到一个字母,程序将返回肯定结果,例如,如果我搜索“f”当字典中没有单词“F”时,它会返回
public static void Option3Method(String dictionary) throws IOException {
Scanner scan = new Scanner(new File("wordlist.txt"));
String s;
String words[] = new String[500];
String word = JOptionPane.showInputDialog("Enter a word to search for");
while (scan.hasNextLine()) {
s = scan.nextLine();
int indexfound = s.indexOf(word);
if (indexfound > -1) {
JOptionPane.showMessageDialog(null, "Word was found");
} else if (indexfound < -1) {
JOptionPane.showMessageDialog(null, "Word was not found");
}
}
}