Search code examples
javanumberformatexception

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

Solution

  • 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.