【问题标题】:Runtime error "java.lang.NumberFormatException" , Not able to rectify运行时错误“java.lang.NumberFormatException”,无法纠正
【发布时间】:2015-08-31 05:13:46
【问题描述】:

我正在尝试实现某些东西,但我无法做到。编译器在运行时不断抛出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());
        }
    }
}

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

【问题讨论】:

  • 编译器在运行时不断抛出异常?

标签: java runtime-error


【解决方案1】:

错误的原因之一是因为您忽略了 JSL 的这一点

如果您查看Integer.parseInt() jsl 它说

Throws:

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

如果字符串到达​​ ENDOFLINE 怎么办。如果已经到达流的末尾,它可以有 字符,如 \r\n 或 \n 即 line terminationnull,这 显然不是“可解析整数”强>。所以你需要在解析之前检查这些。

或者使用java.util.Scanner

【讨论】:

    【解决方案2】:

    你没有给出堆栈跟踪,但我很怀疑你的问题出在了线上:

    size=Integer.parseInt(sizex);
    

    其中sizex 是一个已输入的字符串。如果无法将字符串转换为整数,您将收到 NumberFormatException。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 2017-05-09
      • 2022-11-12
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多