我是java新手,这个项目要求我从输入文件中存储员工的信息,然后打印出来。当我运行程序时,出现“java.lang.NumberFormatException: For input string:”Payroll”,我不知道如何更正。输入的txt文件是:
Zywave Joe Payroll 10.00
RIM Fred Hardware 15.00
Zywave Sally Testing 20.00
RIM Jane Development 30.00
Apple Steve Design 1000.00
谢谢你的帮助!
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class A2Q1
{
public static void main(String[] parms)
{
process();
System.out.println("\nProgram completed normally.");
}
public static void process()
{
Employee[] employees;
employees = loadEmployees();
printEmployees(employees);
}
public static Employee[] loadEmployees()
{
BufferedReader fileIn;
Employee[] employees;
Employee[] newemployees;
String inputLine;
int count;
String [] strings;
count=0;
newemployees = new Employee[50];
try
{
fileIn = new BufferedReader(new FileReader("A2Q1in.txt"));
inputLine = fileIn.readLine();
while (inputLine != null)
{
strings=inputLine.split("\\s+");
newemployees[count]=new Employee(strings[0],strings[1],strings[2],Double.parseDouble(strings[3]));
count++;
inputLine = fileIn.readLine();
}
fileIn.close();
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
}
if (count>0)
{
employees = new Employee[count];
System.arraycopy(newemployees,0,employees,0,count);
}
else
{
employees= new Employee[0];
}
return employees;
}
public static void printEmployees(Employee[] employees)
{
int i;
for (i=0;i<employees.length;i++)
{
System.out.println(employees[i]);
}
}
}
/*********************************************************************/
/*********************************************************************/
class Employee
{
private String group;
private String company;
private double wage;
private String name;
public Employee(String scompany, String sname, String sgroup, double swage)
{
company=scompany;
name = sname;
group = sgroup;
wage=swage;
}
public String toString()
{
return company +" " +name +" " +group+" "+wage;
}
}
这是错误消息:
run A2Q1
java.lang.NumberFormatException: For input string: "Payroll"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at A2Q1.loadEmployees(A2Q1.java:41)
at A2Q1.process(A2Q1.java:18)
at A2Q1.main(A2Q1.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>