简体   繁体   English

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

[英]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. 我正在尝试从文本文件中读取数据,并使用该文件中的某些文本来计算其加班费,并将其输出到名为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: 现在,我的overtime.txt文件的输出如下所示:

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. 我每次加班费都得到0.0,而不是在第一行中打印姓名,而是打印空白行。

Any help with solving this issue of correctly writing the data to the overtime.txt file would be greatly appreciated. 对于解决将数据正确写入overtime.txt文件的任何帮助,将不胜感激。

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). PS-您可能更喜欢使用列表而不是数组,因此大小可以不受限制(在此示例中,超过100名员工将导致ArrayIndexOutOfBounds异常)。 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. 然后,您可以将员工的另一个哈希图作为键,并将每周工作的小时数作为值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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