从文本文件中读取数据以计算数据并将其写入另一个文本文件 [英] Reading data from a text file to compute and write the data in another text file

查看:71
本文介绍了从文本文件中读取数据以计算数据并将其写入另一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件Employee.txt包含两种类型的雇员的雇员详细信息:月薪和小时薪.对于月薪雇员,该文件包含名字,姓氏,性别,职级,类型和基本工资;对于月薪雇员,该文件包含小时工资和工作小时数.该文件的示例如下:

The file Employee.txt contains employee details of two type of employees, monthly-paid and hourlypaid. The file contains the firstname , surname , gender , rank , type and basic salary in case of a monthly-paid employee, or the hourly rate and number of hours worked in case of a hourly-paid employee . A sample of the file is given below:

John Smiths M经理每月45000.00

John Smiths M manager monthly 45000.00

Sunil Bates M senior每小时700.00 45

Sunil Bates M senior hourly 700.00 45

梁伊娃F军官每月30500.00

Eva Leung F officer monthly 30500.00

我必须编写一个程序,该程序将查看每位员工,然后将奖金计算为基本工资的百分比.对于按小时计薪的雇员,将基本工资作为按小时计算的费率乘以该月的工作小时数.如果级别为官",则奖金率为20%;如果级别为高级或更高级别,则奖金率为15%.该程序应将每个雇员的全部详细信息写到新文件TotalSalary.txt中,即名字,姓氏,性别和总工资(即有奖金).

I have to write a program that will look at each employee and calculate the bonus as a percentage of the basic salary. For an hourly-paid employee, the basic salary is taken as the hourly rate times the number of hours worked during the month. If the rank is "officer", the bonus rate is 20% and if the rank is senior or above, the bonus rate is 15 %. The program should write onto a new file TotalSalary.txt the full details of each employee, namely, the first name, surname, gender and total earned salary (i.e with bonus).

这是我的解决方法:

将员工数据写入文本文件类

public class files_qu1 {

public static Formatter writein;
private static Scanner sc;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try{
        writein= new Formatter("Employees.txt");
    }

    catch(SecurityException se){
        System.out.println("You do have access to this file");
        System.exit(1);
    }

    catch(FileNotFoundException fnfe){
        System.out.println("Error in creating file");
        System.exit(1);
    }

    Scanner sc= new Scanner(System.in);

    String fname, lname, gender, rank, type;
    double salary, t_sal, hours=0.0;
    int num;


    System.out.println("Enter num of employees");
    num= sc.nextInt();

    for(int i=0; i<num; i++){
        System.out.println("First name: ");
        fname=sc.next();

        System.out.println("Last name: ");
        lname=sc.next();

        System.out.println("Gender: ");
        gender=sc.next();

        System.out.println("Rank: ");
        rank=sc.next();

        System.out.println("Type: ");
        type=sc.next();

        System.out.println("Salary: ");
        salary=sc.nextDouble();

        if(type.equals("hourly")){
            System.out.println("Hours worked: ");
            hours=sc.nextInt();

        }


        writein.format(" %s %s %s %s %s %.2f %.2f\n",fname, lname, gender, rank, type, salary, hours);
    }

    sc.close();
    if(writein!=null){
        writein.close();
         }


    }

}


从员工文本文件读取和计算数据到TotalSalary文本文件类

public class files_qu1read {

private static Formatter writeToFile;
private static Scanner sc;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        sc= new Scanner(new File("C:/Users/Diksha/workspace/Java_Sem1/Employees.txt"));
    }

    catch
        (FileNotFoundException fnfe){
            System.out.println("Error Creating File");
            System.exit(1);
    }

    String fname = null, lname, gender, rank, type;
    double salary, t_sal = 0, hours;


    while(sc.hasNext()){
        fname=sc.next();
        lname=sc.next();
        gender=sc.next();
        rank=sc.next();
        type=sc.next();
        salary=sc.nextDouble();
        hours=sc.nextDouble();

        if(type.equals("hourly")){

            t_sal= salary*hours;

        }

        if(type.equals("monthly")&&rank.equals("officer")){
            t_sal= salary*1.2;

        }

        if(type.equals("monthly")&&(rank.equals("senior")||rank.equals("manager"))){
            t_sal= salary*1.15;

        }

        try{
            writeToFile = new Formatter("TotalSalary.txt");
        }
        catch (SecurityException se) {
            System.out.println("You do have access to this file");
            System.exit(1);
        }
        catch (FileNotFoundException fnfe) {
            System.out.println("Error Creating File");
            System.exit(1);
        }


        writeToFile.format("First name: %s Last name: %s Gender: %s Total Salary: %.2f",fname,lname, gender, t_sal);
    }



    sc.close();
    if(writeToFile!=null){
        writeToFile.close();
         }


    }

 }

基本上,一切正常,这是该程序能够写入Employees文本文件,但是当涉及到将数据写入TotalSalary文件中时,只有我写到Employees文件中的最后一个雇员的名字正在被使用.写入TotalSalary文件,以及他的总薪水.

Basically, everything works fine that is the program is able to write to the Employees text file but when it comes to reading and writing data to the TotalSalary file, only the last employee's name that I have written to the Employees file is being written to the TotalSalary file and also his total salary.

也就是说,如果Employees.txt文件包含以下数据:

That is if the Employees.txt file has data such as:

Riley Fox f经理每月45000.00 0.00

Riley Fox f manager monthly 45000.00 0.00

艾米莉·罗斯(Emily Roth)军官每小时10000.00 8.00

Emily Roth f officer hourly 10000.00 8.00

在TotalSalary.txt中,显示的唯一数据是最后一位雇员的数据:

In the TotalSalary.txt, the only data being shown is for that of the last employee which is:

名字:Emily姓氏:Roth性别:f总薪资:80000.00

First name: Emily Last name: Roth Gender: f Total Salary: 80000.00

推荐答案

每次创建Formatter写入文件时,都会创建或覆盖该文件.因此,所有内容都会丢失.请阅读Javadoc:

Every time you create the Formatter to write to it the file is created or overwritten. So any content is lost. Please read Javadoc:

  • @param文件名
    • 用作目标文件的文件名
    • 格式化程序.**如果文件存在,它将被截断为
    • 零大小;否则,将创建一个新文件**.输出
    • 将被写入文件并被缓冲.

您可以在循环外创建格式化程序

You can create the formatter outside the loop

 try{
        writeToFile = new Formatter("TotalSalary.txt");
    }
    catch (SecurityException se) {
        System.out.println("You do have access to this file");
        System.exit(1);
    }
    catch (FileNotFoundException fnfe) {
        System.out.println("Error Creating File");
        System.exit(1);
    }
while(sc.hasNext()){

...

并重用它直到程序结束,最后关闭它.您始终可以使用资源尝试格式.

and reuse it until the end of the program and finally close it. You can always use a try-with-resources format.

这篇关于从文本文件中读取数据以计算数据并将其写入另一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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