【发布时间】: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