我正在制作一个程序来初始化每个员工的价值,然后最终显示。我在第4次循环后遇到错误java.lang.IllegalStateException
扫描仪关闭时,扫描仪一直存在问题,任何建议都会有所帮助。
for(int x = 0; x < 5; x++)
{
System.out.println("For an employee who get salary enter #1.");
System.out.println("For an employee who's hourly enter #2.");
System.out.println("For an employee who's paid comission enter #3");
System.out.println("For an employee who's base & comission enter #4 or 0 to quit.");
Employees[x] = keyboard.nextInt();
switch (Employees[x])
{
case 1:
System.out.println("Please enter your first name.");
FName[x] = keyboard.next();
System.out.println("Please enter your last name.");
LName[x] = keyboard.next();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.next();
System.out.println("Please enter your salary amount $.");
Check[x] = keyboard.nextDouble();
SalariedEmployee salariedEmployee =
new SalariedEmployee( FName[x], LName[x], SS[x], Check[x] );
employees[x] = salariedEmployee;
break;
case 2:
System.out.println("Please enter your first name.");
FName[x] = keyboard.nextLine();
System.out.println("Please enter your last name.");
LName[x] = keyboard.nextLine();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.nextLine();System.out.println("Please enter your first name.");
System.out.println("How many hours were worked?");
Hours[x] = keyboard.nextInt();
System.out.println("How much paid per hour?");
Rate[x] = keyboard.nextDouble();
HourlyEmployee hourlyEmployee =
new HourlyEmployee( FName[x], LName[x], SS[x], Hours[x], Rate[x] );
employees[x] = hourlyEmployee;
break;
case 3:
System.out.println("Please enter your first name.");
FName[x] = keyboard.nextLine();
System.out.println("Please enter your last name.");
LName[x] = keyboard.nextLine();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.nextLine();System.out.println("Please enter your first name.");
System.out.println("What was your weekly sale?");
CommissionSales[x] = keyboard.nextDouble();
System.out.println("What is your percentage paid commission?");
CommissionRate[x] = keyboard.nextDouble();
HourlyEmployee hourlyEmployee =
new HourlyEmployee( FName[x], LName[x], SS[x], Hours[x], Rate[x] );
employees[x] = hourlyEmployee;
break;
case 4:
System.out.println("Please enter your first name.");
FName[x] = keyboard.nextLine();
System.out.println("Please enter your last name.");
LName[x] = keyboard.nextLine();
System.out.println("Please enter your social security in format 111-11-1111");
SS[x] = keyboard.nextLine();
System.out.println("What was your weekly sale?");
CommissionSales[x] = keyboard.nextDouble();
System.out.println("What is your percentage paid commission?");
CommissionRate[x] = keyboard.nextDouble();
System.out.println("Please enter your salary amount $.");
Check[x] = keyboard.nextDouble();
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee( FName[x], LName[x], SS[x], CommissionSales[x], CommissionRate[x], Check[x]);
employees[x] = basePlusCommissionEmployee;
break;
}
答案 0 :(得分:1)
如果扫描程序在您尝试使用时关闭,则您使用的所有Scanner
方法都会抛出IllegalStateException
。在代码的某处,您可能有keyboard.close()
。删除这一行,或将其移动到程序的末尾,你就是金色的。来自Java文档:
在扫描程序关闭后尝试执行搜索操作将导致IllegalStateException。