Java 如何从文本文件中读取、处理和打印数据?

Java 如何从文本文件中读取、处理和打印数据?,java,text-files,Java,Text Files,请告诉我这方面需要帮助: A.进一步加强您的薪资计划。添加名为inputData的布尔方法,该方法具有参考参数,用于从payroll.txt文件中输入employeeName、hourlyRate、hoursWorked和taxRate。在方法main中调用方法reportTitle的语句之后,添加一个循环,允许程序读取、处理和打印多个雇员的信息,直到读取和处理完所有数据。您在本练习中编写的程序不计算grossAmount和netAmount;这将在后面的练习中完成。在这个程序中,只需为每个变量

请告诉我这方面需要帮助:

A.进一步加强您的薪资计划。添加名为inputData的布尔方法,该方法具有参考参数,用于从payroll.txt文件中输入employeeName、hourlyRate、hoursWorked和taxRate。在方法main中调用方法reportTitle的语句之后,添加一个循环,允许程序读取、处理和打印多个雇员的信息,直到读取和处理完所有数据。您在本练习中编写的程序不计算grossAmount和netAmount;这将在后面的练习中完成。在这个程序中,只需为每个变量指定0.0

要允许方法inputData读取并返回员工姓名,请将变量employeeName的类型从class String更改为class StringBuffer;您还必须更改printEmployeeInfo中相应方法参数的类型

inputData方法还需要读取和返回hourlyRate、hoursWorked和taxRate的值。要实现这一点,请将变量hourlyRate、hoursWorked和taxRate的类型从基本类型double更改为类DoubleClass。类中定义的构造函数没有用于初始化实例化为0.0的对象的参数。方法setNum()用于使用方法参数设置对象的数据成员。方法getNum()用于检索存储在对象中的双精度值

以下是payroll.txt文件:

John Smith

9.45 40 15

Jane Doe

12.50 45 15

Harry Morgan

20.00 40 20

Carmen Martinez 

25.00 35 25

Jacintha Washington 

50.85 60 34
以下是所需的输出:

                                  Instructions for Payroll Report Program

    This program calculates a paycheck for each employee.
    A text file containing the following information will be created:
    name, pay rate, hours worked, and tax percentage to be deducted.

    The program will create a report in columnar format showing the empoyee name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.

    After all employees are processed, totals will be displayed, including total gross amount and total net pay.


                                          Payroll Report

    Employee                Hourly          Hours           Tax         Gross           Net
    Name                    Rate            Worked          Rate        Amount          Amount
    --------------------    --------        --------        --------    --------        --------
    John Smith                  9.45           40.00           15.00        0.00            0.00
    Jane Doe                   12.50           45.00           15.00        0.00            0.00
    Harry Morgan               20.00           40.00           20.00        0.00            0.00
    Carmen Martinez            25.00           35.00           25.00        0.00            0.00
    Jacintha Washington        50.85           60.00           34.00        0.00            0.00
以下是我目前的代码:

public class Payroll3
{
    final float FULL_TIME = 40;
    public static void main(String[] args)
    {
        StringBuffer employeeName;
        DoubleClass hourlyRate, hoursWorked, taxRate, grossAmount, netAmount;
        instructions();
        reportTitle();
        //printEmployeeInfo(employeeName, hourlyRate, hoursWorked, taxRate,   grossAmount, netAmount);
    }
    public static void instructions()
    {
        System.out.println("\t\t\t\t\t\t\tInstructions for Payroll Report Program\n\nThis program calculates a paycheck for each employee.\nA text file containing the following information will be created:\nname, pay rate, hours worked, and tax percentage to be deducted.\n\nThe program will create a report in columnar format showing the empoyee name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.\n\nAfter all employees are processed, totals will be displayed, including total gross amount and total net pay.\n\n");
    }
    public static void reportTitle()
    {
        System.out.println("\t\t\t\t\t\t\t\tPayroll Report\n\nEmployee            \t\tHourly     \t\tHours       \t\tTax     \t\tGross       \t\tNet       ");
                                      System.out.println("Name\t\t\t\tRate\t\t\tWorked\t\t\tRate\t\t\tAmount\t\t\tAmount");
        System.out.println("--------------------\t\t--------\t\t--------\t\t--------\t\t--------\t\t--------");
    }
    public static void printEmployeeInfo(StringBuffer employeeName, DoubleClass hourlyRate, DoubleClass hoursWorked, DoubleClass taxRate, DoubleClass grossAmount, DoubleClass netAmount)
    {
        System.out.println(employeeName+"\t\t\t    "+hourlyRate+"   "+"\t\t    "+hoursWorked+"\t\t   "+taxRate+"   "+"\t\t  "+grossAmount+"\t\t  "+netAmount+"  OT");
    }
    public static boolean inputData(StringBuffer employeeName, DoubleClass hourlyRate, DoubleClass hoursWorked, DoubleClass taxRate)
    {
        return true;
    }
}

请帮我做下一步。我真的不知道。提前感谢。

使用Scanner类从文件输入,并使用
System.out.printf
格式化输出。 我对您的代码进行了如下修改:

public class Payroll3
{
    final float FULL_TIME = 40;
    public static void main(String[] args) throws FileNotFoundException
    {
        String employeeName;
        Double hourlyRate, hoursWorked, taxRate, grossAmount=0.0, netAmount=0.0;
        instructions();
        reportTitle();
        Scanner sc=new Scanner(new File("payroll.txt"));
        while(sc.hasNext()){
             employeeName=sc.next()+" "+sc.next();
             hourlyRate=sc.nextDouble();
             hoursWorked=sc.nextDouble();
             taxRate=sc.nextDouble();
             printEmployeeInfo(employeeName, hourlyRate, hoursWorked, taxRate,   grossAmount, netAmount);
        }

    }
    public static void instructions()
    {
        System.out.println("\t\t\t\t\t\t\tInstructions for Payroll Report Program\n\nThis program calculates a paycheck for each employee.\nA text file containing the following information will be created:\nname, pay rate, hours worked, and tax percentage to be deducted.\n\nThe program will create a report in columnar format showing the empoyee name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.\n\nAfter all employees are processed, totals will be displayed, including total gross amount and total net pay.\n\n");
    }
    public static void reportTitle()
    {
        System.out.println("\t\t\t\t\t\t\t\tPayroll Report\n\nEmployee            \t\tHourly     \t\tHours       \t\tTax     \t\tGross       \t\tNet       ");
                                      System.out.println("Name\t\t\t\tRate\t\t\tWorked\t\t\tRate\t\t\tAmount\t\t\tAmount");
        System.out.println("--------------------\t\t--------\t\t--------\t\t--------\t\t--------\t\t--------");
    }
    public static void printEmployeeInfo(String employeeName, Double hourlyRate, Double hoursWorked, Double taxRate, Double grossAmount, Double netAmount)
    {
        System.out.printf("%25s%20.2f%20.2f%20.2f%20.2f  OT\n",employeeName,hourlyRate,hoursWorked,taxRate,grossAmount,netAmount);
    }
//    public static boolean inputData(StringBuffer employeeName, Double hourlyRate, Double hoursWorked, Double taxRate)
//    {
//        return true;
//    }
}

下一步是什么?你试过什么?你有什么问题?什么是双层舱?