gpt4 book ai didi

java - 从 .txt 文件读取、计算和写入数据

转载 作者:行者123 更新时间:2023-12-02 04:49:31 25 4
gpt4 key购买 nike

我正在尝试从文本文件中读取数据,并使用该文件中的一些文本来计算他们的加类工资,并将其输出到名为 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 文件的问题,我们将不胜感激。

最佳答案

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

     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++;

关于java - 从 .txt 文件读取、计算和写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29358527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com