リスト形式の単語のテキストファイルを検索する方法を書いています。ユーザーが入力した単語を検索しますが、プログラムによって、1文字でも見つかった場合は肯定的な結果が返されます。たとえば、「f」を検索すると辞書に単語 "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");
}
}
}