将用户输入写入文件 [英] writing user input into a file

查看:87
本文介绍了将用户输入写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想编写一个程序,询问用户有关员工的各种数据 - 例如他们的姓名,职位,年龄和薪水。我希望程序验证年龄和工资是否是正确的数据类型(分别是整数和双精度),并且一旦验证了所有数据,就将其写入文件。这是我到目前为止所做的,它提示用户输入和try和catch块工作,它创建一个文件,但它不写入文件。请帮忙。

Hi i want to write a program that asks the user for various bits of data about an employee - for example their name, job title, age and salary. I want the program to validate that the age and salary are of the correct data types (i.e. integer and double respectively) and once all data has been validated write it to a file. This is what I have done so far, it prompts the user for input and the try and catch block works and it create a file but it doesn't write to the file. Please help.

import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.InputMismatchException;

public class EmployeeFiles {

    public static void main (String[] args)  {
        Scanner input = new Scanner(System.in);
        String employeeName = null;
        String jobTitle = null;
        double salary = 0;
        int age = 0;


        try{
                FileWriter fw = new FileWriter("employeFile.txt");
                PrintWriter pw = new PrintWriter(fw);
                pw.println(employeeName);
                pw.print(age);
                pw.print(salary);
                pw.print(jobTitle);
                pw.flush();
                pw.close();

            }
            catch(IOException e){
                System.out.println("ERROR!");
            }try{

            System.out.println("Please enter name: " );
            employeeName = input.next();
            System.out.println("Please enter salary:");
            salary= input.nextDouble();
            System.out.println("Please enter age: " );
            age = input.nextInt();
            System.out.println("Please enter job title: ");
            jobTitle = input.next();

            input.close();

        }catch(InputMismatchException e) {
            System.out.println("ERROR!! - Invald data type.");
        }


    }
}

推荐答案

尝试以合乎逻辑的步骤思考这个问题。

1.从用户那里获取信息。

2.验证每个输入以确保它是可接受的值。

注意:你可能更喜欢在输入时验证每个项目,而不是获取输入然后一起验证它们。这取决于要检查的值以及它们是否具有相互依赖关系。

3.如果任何验证失败,请返回1并重新开始。

4.如果验证成功,将值写入输出文件。

5.如果需要更多值,请转到1以获取下一组值。

6.如果没有更多输入,关闭所有文件并终止。
Try and think about this in logical steps.
1. Get the information from the user.
2. Validate each input to make sure it is an acceptable value.
Note: you may prefer to validate each item as they are input, rather than getting the inputs and then validating them all together. This will depend on the value to check and whether they have inter-dependencies.
3. If any validation fails, go back to 1 and start again.
4. If validation succeeds, write the values to the output file.
5. If more values required, go to 1 for next set of values.
6. If no more inputs, close all files and terminate.


你的代码工作正常。让我解释你的代码中发生了什么。



first你初始化了员工姓名= null,他的工资和年龄= 0,并将其打印成.txt文件。



所以.txt文件包含

Your code is working fine.Let me explain what is happening in your code.

first you have initialized the employee name = null , his salary and age =0 and made this to print in a .txt file.

So the ".txt" file contains
null
0
0



并且您已要求用户输入无用的数据。

因此您必须删除初始化。(以下给出的参考代码)


and you have asked the user to enter the data which is not useful.
so you have to remove the initialization.(for your reference code given below)

String employeeName;
       String jobTitle;
       double salary ;
       int age;



并删除要求用户输入其详细信息并将其粘贴在此处的数据


and cut the data which ask the user to input his details and paste it above here

 System.out.println("Please enter name: " );
        employeeName = input.next();
        System.out.println("Please enter salary:");
        salary= input.nextDouble();
        System.out.println("Please enter age: " );
        age = input.nextInt();
        System.out.println("Please enter job title: ");
        jobTitle = input.next();

        input.close();
// above here
        try{
                FileWriter fw = new FileWriter("employeFile.txt");
                PrintWriter pw = new PrintWriter(fw);
                pw.println(employeeName);
                pw.print(age);
                pw.print(salary);
                pw.print(jobTitle);
                pw.flush();
                pw.close();
 
            }


这篇关于将用户输入写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆