当我在输入文本字段中输入您的姓名时,为什么它会出现“我的名字是”4次
public void compareQuestion(){
String fileName = directory +“memoryBank.txt”;
String line = null;
try {
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(" - ");
String subject = parts[0];
String question = parts[1];
String answer = parts[2];
//if(input.equals(question) && !subject.equals(currentSubject)){ currentSubject = subject; }
// if(input.equals(question) && subject.equals(currentSubject)){ Output.append(answer); }
if(input.contains("what is your name")) {
Output.append("my name is"+ newline);
}
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
答案 0 :(得分:1)
如果没有一个运行的例子,我相信你的问题就在这里......
while((line = bufferedReader.readLine()) != null) {
String[] parts = line.split(" - ");
String subject = parts[0];
String question = parts[1];
String answer = parts[2];
//if(input.equals(question) && !subject.equals(currentSubject)){ currentSubject = subject; }
// if(input.equals(question) && subject.equals(currentSubject)){ Output.append(answer); }
if(input.contains("what is your name")) {
Output.append("my name is"+ newline);
}
}
实际上,在循环的每次运行中,如果JTextField
input
包含文本what is your name
,它会将String
"my name is"+ newline
附加到文本字段。如果你在文件中有4个问题,它将被追加4次,如果你有100个问题,它将被追加100次。
答案 1 :(得分:0)
在玩了几个小时后,我设法在MadProgrammer的帮助下解决了这个问题。 固定代码:
我无法获得块报价,所以这里是一张图片: