Java 如何将用户输入存储到文本文件中并显示所有数据?
我们已经设法让代码显示第一个员工的详细信息,但是,其他两个员工的详细信息已经显示出来。我不知道如何附加它们。我不确定printWriter是否是发布代码的正确对象,当然不是,那么什么才是最好的呢 代码如下:)Java 如何将用户输入存储到文本文件中并显示所有数据?,java,Java,我们已经设法让代码显示第一个员工的详细信息,但是,其他两个员工的详细信息已经显示出来。我不知道如何附加它们。我不确定printWriter是否是发布代码的正确对象,当然不是,那么什么才是最好的呢 代码如下:) publicstaticvoidmain(字符串[]args)引发IOException{ 扫描仪扫描=新扫描仪(System.in); 文件employeeDetails=新文件(“Employees.txt”); PrintWriter pw=新的PrintWriter(新文件编写器(
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪扫描=新扫描仪(System.in);
文件employeeDetails=新文件(“Employees.txt”);
PrintWriter pw=新的PrintWriter(新文件编写器(employeeDetails,true));
for(int i=0;i所以您的问题是每次在for循环中创建一个新文件(删除另一个)
for(int i=0;i所以您的问题是每次在for循环中创建一个新文件(删除另一个)
for(int i=0;i您不需要fw
:
for(int i=0; i<3; i++){
FileWriter fw = new FileWriter("Employees.txt", true);// remove this line
...
否则您将得到NullPointerException
您不需要fw
:
for(int i=0; i<3; i++){
FileWriter fw = new FileWriter("Employees.txt", true);// remove this line
...
否则您将得到NullPointerException
您的问题是您一直在关闭pw。请将pw的关闭移到for循环之后。我已修改了您的代码,并执行了以下操作:
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();
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪扫描=新扫描仪(System.in);
文件employeeDetails=新文件(“Employees.txt”);
PrintWriter pw=新的PrintWriter(新文件编写器(employeeDetails,true));
对于(int i=0;i<3;i++){
//FileWriter fw=新的FileWriter(“Employees.txt”,true);
试一试{
布尔重复=假;
System.out.println(“输入名称:”);
字符串名称=scan.next();
pw.println(“名称:”+名称);
System.out.println(“输入职务:”);
字符串jobTitle=scan.next();
pw.println(“职务:”+职务);
做{
试一试{
System.out.println(“输入年龄:”;
int age=scan.nextInt();
pw.println(“年龄:+年龄);
重复=正确;
}捕获(输入不匹配异常){
System.err.println(“无效年龄请输入整数”);
scan.next();
继续;
}
}while(repeat==false);
做{
试一试{
System.out.println(“输入每年工资:”);
双倍工资=scan.nextDouble();
pw.println(“工资:+工资”);
重复=错误;
}捕获(输入不匹配异常){
System.err.println(“无效工资,请输入小数点。”);
scan.next();
继续;
}捕获(丢失FormatArgumentException ex){
System.err.println(“无效工资,请输入小数点。”);
scan.next();
继续;
}
}同时(重复);
}最后{
//关闭();
//fw.close();
}
}
关闭();
scan.close();
}
您的问题是您一直在关闭pw。将pw的关闭移动到for循环之后。我已修改了您的代码,并执行了以下操作:
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();
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪扫描=新扫描仪(System.in);
文件employeeDetails=新文件(“Employees.txt”);
PrintWriter pw=新的PrintWriter(新文件编写器(employeeDetails,true));
对于(int i=0;i<3;i++){
//FileWriter fw=新的FileWriter(“Employees.txt”,true);
试一试{
布尔重复=假;
System.out.println(“输入名称:”);
字符串名称=scan.next();
pw.println(“名称:”+名称);
System.out.println(“输入职务:”);
字符串jobTitle=scan.next();
pw.println(“职务:”+职务);
做{
试一试{
System.out.println(“输入年龄:”;
int age=scan.nextInt();
pw.println(“年龄:+年龄);
重复=正确;
}捕获(输入不匹配异常){
System.err.println(“无效年龄请输入整数”);
scan.next();
继续;
}
}while(repeat==false);
做{
试一试{
System.out.println(“输入每年工资:”);
双倍工资=scan.nextDouble();
pw.println(“工资:+工资”);
重复=错误;
}捕获(输入不匹配异常){
System.err.println(“无效工资,请输入小数点。”);
scan.next();
继续;
}捕获(丢失FormatArgumentException ex){
System.err.println(“无效工资,请输入小数点。”);
scan.next();
继续;
}
}同时(重复);
}最后{
//关闭();
//fw.close();
}
}
关闭();
scan.close();
}
fw
有什么用处?你不使用它…当你运行代码时会发生什么?你修复了吗?很抱歉,我有一段时间没有出现堆栈溢出,我已经设法修复了它,谢谢你的帮助。我想,如果最后有文件编写器并为true,它就会附加到我正在编写的文件中。Whw isfw
usOful?你不使用它…当你运行代码时会发生什么?你修复了吗?很抱歉,我已经有一段时间没有出现堆栈溢出,我已经成功修复了它,谢谢你的帮助。我想,通过在末尾使用文件编写器和true,它会附加到我正在编写的文件中。该文件是在循环之外创建的,并且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();
}