[英]Issue with Scanner
I'm making a program that initializes the values for each employee and then it is eventually displayed. 我正在编写一个初始化每个员工的值的程序,然后最终将其显示出来。 I keep having an issue with the scanner after about the 4th loop I get error java.lang.IllegalStateException
scanner closing, any advice would be helpful. 在第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;
}
All of the Scanner
methods you use will throw an IllegalStateException
if the scanner is closed when you attempt to use them. 如果在尝试使用Scanner
方法时将其关闭,则您使用的所有Scanner
方法都将引发IllegalStateException
。 Somewhere in your code, you likely have keyboard.close()
. 在代码中的某处,您可能具有keyboard.close()
。 Delete this line, or move it to the end of your program, and you're golden. 删除此行,或将其移到程序的末尾,您会很高兴。 From the Java documentation: 从Java文档中:
Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException. 关闭扫描仪后尝试执行搜索操作将导致IllegalStateException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.