简体   繁体   中英

NumberFormatException error

I am new to java,and this project asks me to store the information of employees from input files, and print it out. When I run the program, "java.lang.NumberFormatException: For input string: "Payroll" appears, I dont know how to correct it. The input txt file is:

  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

Thanks for your help!

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

}

This is the error message:

 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)
> 

The problem is with your input file

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

because of the space at the beginning of the line, your first element in the splitted array is a blank string and the fourth element(which should be a numeric value) in the array happens to be a string. And when this string is tried to be parsed as a double, it gives the NumberFormatException .

Edit:- Since you can't edit your file, you can use the trim() to help you.

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

That's indicating that something tried to parse a number, but it couldn't because the input string was "Payroll" , which obviously doesn't represent a number. The only method you use that parses numbers is Double.parseDouble , so that's obviously the culprit -- your stack trace verifies that.

Could it be that there's a non-whitespace character in the line Zywave Joe Payroll 10.00 ? A null character ( \ ) or such? Maybe a leading space in the input?

You can check this easily with System.out.println(Arrays.toString(strings)) .

It looks like the problem is here

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

You may have some extra white space in this line

Zywave      Joe  Payroll    10.00

causing Payroll to be passed to

Double.parseDouble(strings[3])

so you're not passing 10.00 as you're expecting.


Not tested yet, but I think you need a non-greedy regex , which will capture all the extra white spaces instead of splitting on each one. It probably looks like this:

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

You could also look into splitting on word boundaries ( \\b if I recall correctly).

From the stack trace, it seems that strings[3] contains the word Payroll instead of the number. This seems to be odd since your split regex looks fine.

The only problem which I am seeing is the formatting and layout of the input file, since it might seem that you have extra white spaces before the text as well. What you could do would be to:

  • Process the line you are reading with something like so: inputline.trim() (before splitting) so that you remove leading and trailing white spaces OR
  • Since the number will always be at the end, you could do this: Double.parseDouble(string[string.length - 1])

Personally I would go for the first option, since it should give you 100% certainty that the only white space you have in the text you are reading is between the values you would like to process, which is essentially an assumption which your code is already running on.

It almost looks like you have tabs and spaces in there. Might want to update your regex to reflect that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM