Tia
Tia

Reputation: 1230

Reading data from a text file to compute and write the data in another text file

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 manager monthly 45000.00

Sunil Bates M senior hourly 700.00 45

Eva Leung F officer monthly 30500.00

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).

Here is how I have worked it out:

Writing employees data to text file class

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();
         }


    }

}

Reading and computing data from the Employees text file to the TotalSalary text file class

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();
         }


    }

 }

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.

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

Riley Fox f manager monthly 45000.00 0.00

Emily Roth f officer hourly 10000.00 8.00

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

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

Upvotes: 1

Views: 2148

Answers (1)

Ricard N&#224;cher Roig
Ricard N&#224;cher Roig

Reputation: 1320

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 fileName
    • The name of the file to use as the destination of this
    • formatter. **If the file exists then it will be truncated to
    • zero size; otherwise, a new file will be created**. The output
    • will be written to the file and is buffered.

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.

Upvotes: 1

Related Questions