我打开了一个文件,它打印得很好,看起来有点像这样;
John Hop 13 12.00
Laura Shack 6 15.00
但是有一个6的数组。由于我有另一个带有getter和setter的类,该文件能够得到每个人的总薪水:
John 13 12.00 150.00
Laura Shack 6 15.00 120.00
我想打印所有个人的总薪水,然后获得平均值。 这是我的一些代码
public class PayRoll {
public static void main(String[] args) {
final int NUMBER_OF_WORKERS = 15;
final String INPUT_FILE = "data.txt";
Worker[] worker_ar = new Worker[NUMBER_OF_WORKERS];
Scanner file = null;
try {
file = new Scanner(new File(INPUT_FILE));
} catch (FileNotFoundException e) {
System.err.println("File Not Found!");
}
String line = null;
int count = 0;
double total_pay = 0;
double avg_pay = 0;
while ((file.hasNextLine()) && (count < worker_ar.length)) {
String fName = file.next();
String lName = file.next();
int hours = file.nextInt();
double hrly_pay = file.nextDouble();
worker_ar[count] = new Worker(fName, lName, hours, hrly_pay);
count++;
//Here, i tried computing the average but it is wrong.
for (int i = 0; i < count; i++)
total_pay = total_pay + worker_ar[i].computePay();
avg_pay = total_pay / count;
答案 0 :(得分:1)
在计算总数和平均值(或至少是平均值)之前,完成文件内容的读取。像
这样的东西TimeoutException