問題描述
如何顯示多個用戶輸入的答案以及如何將其全部寫入文本文件 (How to display several user inputted answers and how to write it all to a text file)
所以我的代碼遇到了另一個問題。我想要做的是能夠輸出用戶輸入的不同條目。我還希望能夠將所有所述條目輸出到 .txt 文件中。稍後我將在 Microsoft Excel 中使用它。到目前為止,這是我的代碼:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.text.*;
import java.io.*;
public class Project1{
public static void main(String[] args) throws FileNotFoundException
{
String fileName = "out.txt";
try {
Scanner keyboard = new Scanner(System.in);
Scanner user_input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
PrintWriter outFile = new PrintWriter("Project1.out");
String employee_Fname;
String employee_Lname;
String employee_city;
String employee_state;
double empzip;
String employee_job;
double empsal;
char again;
int count = 1;
String answer;
do {
System.out.print("Enter Employees First Name: ");
employee_Fname = user_input.next();
System.out.println();
System.out.print("Enter the employee's last name: ");
employee_Lname = user_input.next();
System.out.println();
System.out.print("Enter employee's city: ");
employee_city = user_input.next();
System.out.println();
System.out.print("Enter employee's state: ");
employee_state = user_input.next();
employee_state.toUpperCase();
System.out.println();
System.out.print("Enter employee's zipcode: ");
empzip = keyboard.nextDouble();
System.out.println();
System.out.print("Enter employee's job title: ");
employee_job = user_input.next();
System.out.println();
System.out.print("Enter employee's salary: ");
empsal = keyboard.nextDouble();
System.out.println();
while(empsal > 2000000) {
System.out.println();
System.out.println("Invalid salary entered! Please tryn again.");
System.out.print("Enter employee's salary: ");
empsal = keyboard.nextDouble();
System.out.println();
System.out.println();
}
System.out.print("Do you want to enter another employee? Y/N?");
answer = keyboard.next();
} while (answer.equals("Y"));
outFile.printf("Employee first name is: %n "+ employee_Fname +"%n");
outFile.printf("Employee last name is: %n " + employee_Lname +"%n");
outFile.printf("Employee city is: %n " + employee_city + "%n");
outFile.printf("Employee state is: %n " + employee_state +"%n");
outFile.printf("Employee zipcode is: %n " + empzip + "%n");
outFile.printf("Employee job is: %n " + employee_job +"%n");
outFile.printf("Employee salary is: %n " + empsal +"%n");
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
然而,輸出甚至沒有給我一個文本文件,如果我輸入第二個條目,它會刪除我之前的條目,只顯示最後輸入的條目。也許我把“嘗試”提早了?
編輯:嘿,別管我想出來的文本部分!原來我沒有正確定義“文件名”。但是,我的程序仍然有問題。我仍然需要幫助,因為它不會讓我顯示除最後輸入的條目之外的任何其他內容。不僅如此,txt文件也是空的。我很高興我至少可以顯示 txt 文件,但它是空的。
`
參考解法
方法 1:
You need to print the results to the file before you move onto the next employee. Also, you need to use a PrintWriter that takes a FileWriter, and ultimately a File so that you can append instead of overwritting the contents as another user noted.
public static void main(String[] args) {
String fileName = "out.txt";
try {
Scanner keyboard = new Scanner(System.in);
Scanner user_input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
String employee_Fname;
String employee_Lname;
String employee_city;
String employee_state;
double empzip;
String employee_job;
double empsal;
char again;
int count = 1;
String answer;
do {
System.out.print("Enter Employees First Name: ");
employee_Fname = user_input.next();
System.out.println();
System.out.print("Enter the employee's last name: ");
employee_Lname = user_input.next();
System.out.println();
System.out.print("Enter employee's city: ");
employee_city = user_input.next();
System.out.println();
System.out.print("Enter employee's state: ");
employee_state = user_input.next();
employee_state.toUpperCase();
System.out.println();
System.out.print("Enter employee's zipcode: ");
empzip = keyboard.nextDouble();
System.out.println();
System.out.print("Enter employee's job title: ");
employee_job = user_input.next();
System.out.println();
System.out.print("Enter employee's salary: ");
empsal = keyboard.nextDouble();
System.out.println();
while (empsal > 2000000) {
System.out.println();
System.out.println("Invalid salary entered! Please tryn again.");
System.out.print("Enter employee's salary: ");
empsal = keyboard.nextDouble();
System.out.println();
System.out.println();
}
try (PrintWriter outFile = new PrintWriter(new FileWriter(new File("Project1.out"), true))) {
outFile.printf("Employee first name is: %n " + employee_Fname + "%n");
outFile.printf("Employee last name is: %n " + employee_Lname + "%n");
outFile.printf("Employee city is: %n " + employee_city + "%n");
outFile.printf("Employee state is: %n " + employee_state + "%n");
outFile.printf("Employee zipcode is: %n " + empzip + "%n");
outFile.printf("Employee job is: %n " + employee_job + "%n");
outFile.printf("Employee salary is: %n " + empsal + "%n");
}
System.out.print("Do you want to enter another employee? Y/N?");
answer = keyboard.next();
} while (answer.equals("Y"));
} catch (IOException e) {
e.printStackTrace();
}
}