简体   繁体   中英

Reading, Calculating and Writing Data To/From .txt File

I'm trying to read data from a text file, and use some of the text from that file to calculate their overtime pay, and output it to a file called overtime.txt.

The data file would look like this:

bob
50
2.3
julio
60
60.00

My code so far is below:

import javax.swing.JOptionPane;

import java.io.*;
import java.text.DecimalFormat;

public class ReadData { 
public ReadData ()
{
    try {
         String[] firstLine = new String[100],
         secondLine = new String[100],
         thirdLine = new String[100];

         double hours[] = new double[100], wages[] = new double[100];
         String result[] = new String[100],  formattedGrossPay[] = new String[100];
         double grossPay[] = new double[100];
         double excessHours[] = new double[100], extraPay[] = new double[100];
         String stringExcessHours[] = new String[100];

         int index;

         for (index = 0; index < 100; index++) {
         firstLine[index] = "";
         secondLine[index] = "";
         thirdLine[index ] = "";
         hours[index] = 0.0;
         wages[index]= 0.0;
    }
     FileReader file = new FileReader("payroll.txt");
     BufferedReader buffer = new BufferedReader(file);
     index = 0;
     String line;

     File check = new File("overtime.txt");
     FileWriter file2;
     if(check.exists())
         file2 = new FileWriter("overtime.txt", true);
     else
         file2 = new FileWriter("overtime.txt");
     BufferedWriter buffer2 = new BufferedWriter(file2);

     /*
     FileWriter file1 = new FileWriter("overtime.txt");
     BufferedWriter buffer1 = new BufferedWriter(file1);
     */

     while((line = buffer.readLine()) != null)
     {
         firstLine[index] = line;
         secondLine[index] = buffer.readLine();
         thirdLine[index ] = buffer.readLine();

         hours[index] = Double.parseDouble(secondLine[index]);
         wages[index] = Double.parseDouble(thirdLine[index]);
         DecimalFormat df = new DecimalFormat("#.00");
         result[index] = df.format(wages[index]);
         grossPay[index] = (hours[index] * wages[index]);
         if (hours[index] > 40)
         {
            excessHours[index] = hours[index]-40;
            extraPay[index] = excessHours[index] * (.5 * wages[index]);
            grossPay[index]=grossPay[index]+extraPay[index];
         }
         else
         {
             excessHours[index]=0;
         }

         //grossPayString[index] = Double.toString(grossPay[index]);
         formattedGrossPay[index] = df.format(grossPay[index]);

         JOptionPane.showMessageDialog(null, "Name: " + firstLine[index] + "\nHours: "
         + hours[index] + "\nWages: $" + result[index] + "\nGross Pay: $" + formattedGrossPay[index], "Result",
         JOptionPane.PLAIN_MESSAGE );
         index++;

         stringExcessHours[index] = Double.toString(excessHours[index]);
         buffer2.write(firstLine[index]);
         buffer2.newLine();
         buffer2.write(stringExcessHours[index]);
         buffer2.newLine();
     }

     buffer.close();
     buffer2.close();
     System.exit(0);

    }//end of try
    catch (IOException e ) { System.out.println(e); }
    }//end of ReadData

public static void main(String[] args)
{
    new ReadData();
}
}

Right now, the output to my overtime.txt file would look like this:

0.0

0.0

I get 0.0 every time for their overtime pay, and instead of printing the name in the first line, a blank line is printed.

Any help with solving this issue of correctly writing the data to the overtime.txt file would be greatly appreciated.

In you code you are incrementing the index then reading the array

as in

     index++;

     stringExcessHours[index] = Double.toString(excessHours[index]);
     buffer2.write(firstLine[index]);
     buffer2.newLine();
     buffer2.write(stringExcessHours[index]);
     buffer2.newLine();

change to

     stringExcessHours[index] = Double.toString(excessHours[index]);
     buffer2.write(firstLine[index]);
     buffer2.newLine();
     buffer2.write(stringExcessHours[index]);
     buffer2.newLine();

     index++;

This works:

import javax.swing.JOptionPane;
import java.io.*;
import java.text.DecimalFormat;

public class ReadData { 
    public ReadData ()
    {
        try {
            String[] firstLine = new String[100],
            secondLine = new String[100],
            thirdLine = new String[100];

            double hours[] = new double[100], wages[] = new double[100];
            String result[] = new String[100],  formattedGrossPay[] = new String[100];
            double grossPay[] = new double[100];
            double excessHours[] = new double[100], extraPay[] = new double[100];
            String stringExcessHours[] = new String[100];

            int index;

            for (index = 0; index < 100; index++) {
                firstLine[index] = "";
                secondLine[index] = "";
                thirdLine[index ] = "";
                hours[index] = 0.0;
                wages[index]= 0.0;
            }
            FileReader file = new FileReader("payroll.txt");
            BufferedReader buffer = new BufferedReader(file);
            index = 0;
            String line;

            File check = new File("overtime.txt");
            FileWriter file2;
            if(check.exists())
                file2 = new FileWriter("overtime.txt", true);
            else
                file2 = new FileWriter("overtime.txt");

            BufferedWriter buffer2 = new BufferedWriter(file2);

        /*
            FileWriter file1 = new FileWriter("overtime.txt");
            BufferedWriter buffer1 = new BufferedWriter(file1);
            */

            while((line = buffer.readLine()) != null)
            {
                firstLine[index] = line;
                String name = firstLine[index];

                secondLine[index] = buffer.readLine();
                int hoursWorked = Integer.parseInt(secondLine[index]);

                thirdLine[index ] = buffer.readLine();
                double wage = Double.parseDouble(thirdLine[index]);

                hours[index] = hoursWorked;
                wages[index] = wage;
                DecimalFormat df = new DecimalFormat("#.00");
                result[index] = df.format(wages[index]);
                grossPay[index] = (hours[index] * wages[index]);

                if (hours[index] > 40)
                {
                    excessHours[index] = hours[index]-40;
                    extraPay[index] = excessHours[index] * (.5 * wages[index]);
                    grossPay[index]=grossPay[index]+extraPay[index];
                }
                else
                {
                    excessHours[index]=0;
                }

                //grossPayString[index] = Double.toString(grossPay[index]);
                formattedGrossPay[index] = df.format(grossPay[index]);

                JOptionPane.showMessageDialog(null, "Name: " + firstLine[index] + "\nHours: "
                    + hours[index] + "\nWages: $" + result[index] + "\nGross Pay: $" + formattedGrossPay[index], "Result",
                JOptionPane.PLAIN_MESSAGE );


                stringExcessHours[index] = Double.toString(excessHours[index]); 

                buffer2.write(name);
                buffer2.newLine();
                buffer2.write("Excess Hours: " + stringExcessHours[index] + "\tExtra pay: " + extraPay[index] );
                buffer2.newLine();
                index++;
            }

            buffer.close();
            buffer2.close();
            System.exit(0);

        }//end of try
        catch (IOException e ) { System.out.println(e); }
    }//end of ReadData

    public static void main(String[] args)
    {
        new ReadData();
    }
}

PS - You might rather use lists than arrays so the sizes can be unlimited (in this example, over 100 employees would cause an ArrayIndexOutOfBounds exception). You might also want to use hash maps instead of lists, with employees as keys and their wages as the values. Then you could have another hash map of employees as keys and their hours worked each week as the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM