[英]How store user input into a text file and display all the data?
我們設法獲取代碼以顯示第一個員工的詳細信息,但是,已顯示其他2個員工詳細信息。 我不知道如何追加它們。 我不確定printWriter是否是正確的代碼,而不是那么,什么是最好的?
代碼如下:)
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(System.in);
File employeeDetails = new File("Employees.txt");
PrintWriter pw = new PrintWriter(new FileWriter(employeeDetails, true));
for(int i=0; i<3; i++){
FileWriter fw = new FileWriter("Employees.txt", true);
try{
boolean repeat = false;
System.out.println("Enter name: ");
String name = scan.next();
pw.println("name: " + name);
System.out.println("Enter job title: ");
String jobTitle = scan.next();
pw.println("Job title: " + jobTitle);
do{
try{
System.out.println("Enter age: ");
int age = scan.nextInt();
pw.println("Age: " + age);
repeat = true;
}
catch(InputMismatchException ex){
System.err.println("Invalid age please enter a whole number.");
scan.next();
continue;
}
}while(repeat==false);
do{
try{
System.out.println("Enter salary per year: ");
double salary = scan.nextDouble();
pw.println("Salary: " + salary);
repeat = false;
}
catch(InputMismatchException ex){
System.err.println("Invalid salary please enter a decimal.");
scan.next();
continue;
}
catch(MissingFormatArgumentException ex){
System.err.println("Invalid salary please enter a decimal.");
scan.next();
continue;
}
}while(repeat);
}finally{
pw.close();
fw.close();
}
}
scan.close();
}
}
所以你的問題是,每次在for循環中你創建一個新文件(刪除另一個)
for(int i=0; i<3; i++){
FileWriter fw = new FileWriter("Employees.txt", true);
把它放在外面
你不需要fw
:
for(int i=0; i<3; i++){
FileWriter fw = new FileWriter("Employees.txt", true);// remove this line
...
並刪除此行:
fw.close();
否則你會得到NullPointerException
你的問題是你繼續關閉pw。 在for循環后移動pw的關閉。 我修改了你的代碼和以下工作:
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
File employeeDetails = new File("Employees.txt");
PrintWriter pw = new PrintWriter(new FileWriter(employeeDetails, true));
for (int i = 0; i < 3; i++) {
//FileWriter fw = new FileWriter("Employees.txt", true);
try {
boolean repeat = false;
System.out.println("Enter name: ");
String name = scan.next();
pw.println("name: " + name);
System.out.println("Enter job title: ");
String jobTitle = scan.next();
pw.println("Job title: " + jobTitle);
do {
try {
System.out.println("Enter age: ");
int age = scan.nextInt();
pw.println("Age: " + age);
repeat = true;
} catch (InputMismatchException ex) {
System.err.println("Invalid age please enter a whole number.");
scan.next();
continue;
}
} while (repeat == false);
do {
try {
System.out.println("Enter salary per year: ");
double salary = scan.nextDouble();
pw.println("Salary: " + salary);
repeat = false;
} catch (InputMismatchException ex) {
System.err.println("Invalid salary please enter a decimal.");
scan.next();
continue;
} catch (MissingFormatArgumentException ex) {
System.err.println("Invalid salary please enter a decimal.");
scan.next();
continue;
}
} while (repeat);
} finally {
//pw.close();
//fw.close();
}
}
pw.close();
scan.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.