我想驗證我的文本文件是否已經包含用戶在文本字段中輸入的單詞。當用戶點擊驗證,如果單詞已經在文件中,用戶將輸入另一個單詞。如果單詞不在文件中,則會添加單詞。我的文件的每一行都包含一個單詞。我把System.out.println看到正在打印什麼,它總是說這個詞不存在於文件中,但它不是真的......你能告訴我什麼是錯的嗎?(JAVA)比較用戶輸入的單詞與文本文件中包含的另一個單詞
謝謝。
class ActionCF implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
str = v[0].getText();
BufferedWriter out;
BufferedReader in;
String line;
try
{
out = new BufferedWriter(new FileWriter("D:/File.txt",true));
in = new BufferedReader(new FileReader("D:/File.txt"));
while ((line = in.readLine()) != null)
{
if ((in.readLine()).contentEquals(str))
{
System.out.println("Yes");
}
else {
System.out.println("No");
out.newLine();
out.write(str);
out.close();
}
}
}
catch(IOException t)
{
System.out.println("There was a problem:" + t);
}
}
}
哪些內容Ø f您正在使用的文件,您的輸入是什麼,控制檯吐出了什麼? –
您是否嘗試過使用掃描儀?我總是比較喜歡掃描儀。 – buch11
嗨尼古拉斯。每行文本文件包含1個字。用戶在textField中輸入一個單詞,並且我想知道這個單詞是否已經在文本文件中,如果沒有,請添加它。 – ARH