【发布时间】:2014-05-01 06:59:33
【问题描述】:
我收到一条消息: 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:1 尝试运行以下代码时。该程序从文本文件中获取输入,处理一些工资数字,并输出他们的总工资。
这是文本文件的内容: http://m.uploadedit.com/b034/139892732049.txt
package payroll;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class PayRoll
{
private String empName;
private int hours;
private int hourlyPayRate;
public PayRoll(String name, int hh, int rr)
{
empName = name;
hours = hh;
hourlyPayRate = rr;
}
public String getName()
{
return empName;
}
public double getPay()
{
if(hours <= 40)
return hours * hourlyPayRate;
else
return (40 * hourlyPayRate) + (hours - 40) * 1.5 * hourlyPayRate;
}
public static void main(String[] args)
{
Scanner inFile = null;
try
{
inFile = new Scanner(new File("payroll.txt"));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
String name;
String first;
String last;
int hh;
int rr;
String result = "Details of employees:\n";
while(inFile.hasNextLine())
{
String line = inFile.nextLine();
String tokens[] = line.split(" ");
first = tokens[0];
last = tokens[1];
hh = Integer.parseInt(tokens[2]);
rr = Integer.parseInt(tokens[3]);
name = first + " " + last;
PayRoll payroll = new PayRoll(name, hh, rr);
result += "Name: " + payroll.getName() + ", GrossPay: $" + payroll.getPay() + "\n";
}
JOptionPane.showMessageDialog(null, result);
inFile.close();
}
}
【问题讨论】:
-
哪一行代码和你的数据文件导致异常?
标签: java swing indexoutofboundsexception