我的文件正在编译时没有错误,但是,当用户输入" no"作为他们的答案,循环重复(而不是退出程序,我必须退出程序由于无限循环)。其他一切正常,我的新联系人被添加到名为" contactList.csv"的文件中。
我应该考虑使用不同的循环来获得参数吗?
import java.util.*;
import java.io.*;
class ContactList {
List<Contact> people = new ArrayList<Contact>();
Scanner input= new Scanner(System.in);
Contact contact;
public void addContact() throws IOException{
Scanner input= new Scanner(System.in);
String answer;
ArrayList<String> people = new ArrayList<>();
do{
Contact one=new Contact();
people.add((Arrays.toString(one.getEverything())));
String FileName="contactList.csv";
for(int i=0;i<people.size();i++){
System.out.println(people.get(i));
// added a new file in the loop to worite to
try {
PrintWriter outfile=new PrintWriter(new FileWriter(FileName,true));
outfile.println(people.get(i)+"\n");
outfile.close();
} catch (FileNotFoundException ex) {
}
}
System.out.println("Would you like to create another contact?");
answer=input.next();
}while(answer.equalsIgnoreCase("yes"));
}
//rest of the code...
}