读取,计算和写入.txt文件的数据

时间:2015-03-31 00:18:53

标签: java bufferedreader filewriter

我试图从文本文件中读取数据,并使用该文件中的一些文本来计算加班费,并将其输出到名为overtime.txt的文件中。

数据文件如下所示:

bob
50
2.3
julio
60
60.00

到目前为止我的代码如下:

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

现在,我的overtime.txt文件的输出如下所示:

0.0

0.0

我每次都得到0.0加班费,而不是在第一行打印名称,打印出一个空行。

任何帮助解决将数据正确写入overtime.txt文件的问题都将非常感激。

2 个答案:

答案 0 :(得分:1)

在您的代码中,您正在递增索引,然后读取数组

,如

     index++;

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

更改为

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

     index++;

答案 1 :(得分:1)

这有效:

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

P.S。 - 您可能更喜欢使用列表而不是数组,因此大小可以是无限的(在此示例中,超过100名员工会导致ArrayIndexOutOfBounds异常)。您可能还希望使用哈希映射而不是列表,员工作为键,工资作为值。然后,您可以将另一个员工哈希映射作为键,并将每周工作时间作为值。