NumberFormatException错误

时间:2013-10-17 04:55:20

标签: java numberformatexception

我是java的新手,这个项目要求我从输入文件中存储员工的信息,并将其打印出来。当我运行程序时,“java.lang.NumberFormatException:对于输入字符串:”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)
> 

5 个答案:

答案 0 :(得分:0)

问题在于您的输入文件

Zywave      Joe  Payroll    10.00
RIM     Fred    Hardware    15.00
Zywave Sally Testing    20.00 // Remove the extra space at the start of this line
RIM Jane   Development 30.00 // Remove the extra space at the start of this line
Apple  Steve  Design    1000.00

由于行开头的空格,分割数组中的第一个元素是空字符串,数组中的第四个元素(应该是数值)恰好是一个字符串。当尝试将此字符串解析为double时,它会提供NumberFormatException

修改: - 由于您无法修改文件,因此可以使用trim()为您提供帮助。

while (inputLine != null) {
    inputLine = inputLine.trim(); // This will remove the leading and trailing spaces for you. Job done.
    strings = inputLine.split("\\s+");
    ...
}

答案 1 :(得分:0)

这表明某些东西试图解析一个数字,但它不能,因为输入字符串是"Payroll",这显然不代表数字。解析数字的唯一方法是Double.parseDouble,所以这显然是罪魁祸首 - 你的堆栈跟踪验证了这一点。

可能是行Zywave Joe Payroll 10.00中有非空白字符吗?空字符(\u0000)还是这样?也许是输入中的领先空间?

您可以使用System.out.println(Arrays.toString(strings))轻松检查此内容。

答案 2 :(得分:0)

看起来问题就在这里

strings=inputLine.split("\\s+");

此行中可能有一些额外的空白

Zywave      Joe  Payroll    10.00

导致Payroll传递给

Double.parseDouble(strings[3])

所以你没有像你期望的那样传递10.00


尚未测试,但我认为你需要一个non-greedy regex,它将捕获所有额外的空格而不是每个空格。它可能看起来像这样:

...split("(\\s+)?");

如果我没记错的话,您还可以考虑分割单词边界(\b)。

答案 3 :(得分:0)

从堆栈跟踪中,strings[3]似乎包含单词Payroll而不是数字。这似乎很奇怪,因为你的split正则表达式看起来很好。

我看到的唯一问题是输入文件的格式和布局,因为看起来你在文本之前也有额外的空格。你能做的是:

  • 使用类似的方式处理您正在阅读的行:inputline.trim()(在拆分之前),以便删除前导和尾随空格或
  • 由于号码总是在最后,您可以执行此操作:Double.parseDouble(string[string.length - 1])

就我个人而言,我会选择第一个选项,因为它应该让您100%确定您正在阅读的文本中唯一的空白区域是您要处理的值之间,这实际上是您的假设代码已经在运行。

答案 4 :(得分:0)

它几乎看起来像你有标签和空格。可能想要更新你的正则表达式以反映这一点。

相关问题