运行时错误" java.lang.NumberFormatException" ,无法纠正

时间:2015-06-15 15:58:53

标签: java runtime-error

我正在尝试实施某些功能,但我无法做到。编译器在运行时不断抛出java.lang.NumberFormatException。 这是我的代码:

import java.util.Arrays;
import java.io.* ;

public class Staff {

    public static void main(String[] args) throws Exception {
        try {

            //Array declaration
            ManagementEmployee [] eManager=null;
            EngineeringEmployee [] eEngineer=null;
            AdministrationEmployee [] eAdmin=null;

            //Attributes- local
            int size=-1;
            boolean success=false;
            int contractID=0;
            double totalSalary=0.0;
            double eManagerSalary=0.0;
            double eEngineerSalary=0.0;
            double eAdminSalary=0.0;
            String sizex="1";

            //Attributes- Company 
           // name: the name of the employee, a String
            String name;
            //* contract: the type of contract, an int 
            ContractType contract=ContractType.TEMPORARY;
            //* years: number of years in the company, an int 
            int years;
            //* department: department to which he/she belongs, a String


            InputStreamReader istream = new InputStreamReader(System.in) ;
              BufferedReader in = new BufferedReader(istream) ;

           //Managers
           while(!success)
           {
                System.out.println("Enter no. of Management Employees: ");
                sizex=in.readLine();
                size=Integer.parseInt(sizex);
                if(size<0)
                {
                    throw new IllegalArgumentException("Invalid input received. There must be atleast 1 manager");
                }
                else
                {
                    eManager=new ManagementEmployee[size];
                    success=true;
                }
           }
           success=false;
           //Engineers 
           while(!success)
           {
                System.out.println("Enter no. of Engineering Employees: ");
                sizex=in.readLine();
                size=Integer.parseInt(sizex);
                if(size<0)
                {
                    throw new IllegalArgumentException("Invalid input received. There must be atleast 1 engineering employee");
                }
                else
                {
                    eEngineer=new EngineeringEmployee[size];
                    success=true;
                }
           }
           success=false;
            //Administrator
            while(!success)
            {
                System.out.println("Enter no. of Administrator Employees: ");
                sizex=in.readLine();
                size=Integer.parseInt(sizex);
                if(size<0)
                {
                    throw new IllegalArgumentException("Invalid input received. There must be atleast 1 administrator");
                }
                else
                {
                    eAdmin=new AdministrationEmployee[size];
                    success=true;
                }
            }
            success=false;

            //Management
            System.out.println("Please enter the following details:");
            while(!success)
            {
                System.out.println("Management Employee :-");

                for(int i=0;i<eManager.length;i++)
                {
                    System.out.println("Enter the name: ");
                    name=in.readLine();
                    if(name.trim().length() < 0)
                    {
                        throw new IllegalArgumentException("Invalid name. Please enter again"); 
                    }
                    System.out.println("Enter the no. of years in company: ");
                    sizex=in.readLine();
                    years=Integer.parseInt(sizex);
                    if(years<1)
                    {
                        throw new IllegalArgumentException("Invalid input received. It must be more than or equal to 1 yr.");
                    }

                    eManager[i]=new ManagementEmployee(name,years);
                    System.out.println(eManager[i].toString());
                    eManagerSalary+=eManager[i].getSalary();
                }
                success=true;
            }

            success=false;
            //Engineers
            while(!success)
            {
                System.out.println("Engineering Employee :-");

                for(int i=0;i<eEngineer.length;i++)
                {
                    System.out.println("Enter the name: ");
                    name=in.readLine();
                    if(name.trim().length() < 0)
                    {
                        throw new IllegalArgumentException("Invalid name. Please enter again");
                        //System.out.println("Enter the name: ");
                       // name=in.nextLine();
                    }
                    System.out.println("Enter the no. of years in company: ");
                    sizex=in.readLine();
                    years=Integer.parseInt(sizex);
                    if(years<1)
                    {
                        throw new IllegalArgumentException("Invalid input received. It must be more than or equal to 1 yr.");
                        //System.out.println("Enter the no. of years in company: ");
                        //years=in.nextInt();
                    }
                    System.out.println("Enter the contract type from following:\n 1.TEMPORARY\n2. TRAINING\n3. INDEFINITE: ");
                    sizex=in.readLine();
                    contractID=Integer.parseInt(sizex);
                    if(contractID<1||contractID>3)
                    {
                        throw new IllegalArgumentException("Invalid input received. It must be 1 , 2 or 3.");
                        //System.out.println("Enter the contract type from following:\n 1.TEMPORARY\n2. TRAINING\n3. INDEFINITE: ");
                        //contractID=in.nextInt();
                    }
                    if(contractID==1)
                    {

                        eEngineer[i]=new EngineeringEmployee(name,contract.TEMPORARY,years);
                    }
                    else if(contractID==2)
                    {
                        eEngineer[i]=new EngineeringEmployee(name,contract.TRAINING,years);
                    }
                    else
                    {
                        eEngineer[i]=new EngineeringEmployee(name,contract.INDEFINITE,years);

                    }

                    System.out.println(eEngineer[i].toString());
                    eEngineerSalary+=eEngineer[i].getSalary();
                }
                    success=true;
            }

            success=false;
            //Administrator
            while(!success)
            {
                System.out.println("Administrator Employee :-");


                for(int i=0;i<eAdmin.length;i++)
                {
                    System.out.println("Enter the name: ");
                    name=in.readLine();
                    if(name.trim().length() < 0)
                    {
                        throw new IllegalArgumentException("Invalid name. Please enter again"); 
                        //System.out.println("Enter the name: ");
                        //name=in.nextLine();
                    }
                    System.out.println("Enter the no. of years in company: ");
                    sizex=in.readLine();
                    years=Integer.parseInt(sizex);
                    if(years<1)
                    {
                        throw new IllegalArgumentException("Invalid input received. It must be more than or equal to 1 yr.");
                        //System.out.println("Enter the no. of years in company: ");
                        //years=in.nextInt();
                    }
                    eAdmin[i]=new AdministrationEmployee(name,years);
                    System.out.println(eAdmin[i].toString());
                    eAdminSalary+=eAdmin[i].getSalary();
                }
                success=true;


            }

            //Total Salary
            totalSalary=eManagerSalary+eEngineerSalary+eAdminSalary;

            System.out.println("MANAGEMENT TOTAL SALARY:"+eManagerSalary+" bitcoins");
            System.out.println("ENGINEERING TOTAL SALARY:"+eEngineerSalary+" bitcoins");
            System.out.println("ADMINISTRATION  TOTAL SALARY:"+eAdminSalary+" bitcoins");
            System.out.println("ACME TOTAL SALARY:"+totalSalary+" bitcoins");
        }
        catch (NumberFormatException err) {
            System.out.println(err.getMessage());
        }
    }
}

此代码已成功编译,但在运行时遇到错误 如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

错误的原因之一是因为你忽视了JSL

如果你看一下Integer.parseInt() jsl 它说

Throws:

NumberFormatException - if the string does not contain a parsable integer.

那么当String到达ENDOFLINE时会怎样。它可以有字符,如\ r \ n或\ n,如line terminationnull ,如果已达到流的结尾,显然不是&# 34;可解析的整数&#34; 。因此,您需要在解析之前检查这些内容。

或使用java.util.Scanner

答案 1 :(得分:1)

您没有给出堆栈跟踪,但我非常怀疑您的问题是在线:

size=Integer.parseInt(sizex);

其中sizex是已输入的String。如果无法将String转换为整数,则会得到NumberFormatException。