我想验证我的文本文件是否已经包含用户在文本字段中输入的单词。当用户点击验证,如果单词已经在文件中,用户将输入另一个单词。如果单词不在文件中,则会添加单词。我的文件的每一行都包含一个单词。我把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