java - 扫描仪问题

标签 java java.util.scanner

我正在制作一个程序,初始化每个员工的值,然后最终显示出来。在大约第四个循环之后,我一直遇到扫描仪问题,我收到错误 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;
    }       

最佳答案

如果您在尝试使用扫描仪时关闭了扫描仪,那么您使用的所有Scanner方法都会抛出IllegalStateException。在代码中的某个位置,您可能有 keyboard.close()。删除这一行,或者将其移动到程序的末尾,就可以了。来自 Java 文档:

Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException.

关于java - 扫描仪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188433/

相关文章:

Java:使用 Scanner 类从文件中读取输入而无需管道

java - 如何在Java中循环遍历多个输入行

java - JBoss 5 perm 大小和堆大小

java - 如何从jsp页面/java程序调用自定义sql包

Java Swing JScrollPane 显示的输出与书中显示的不同。行标签在 Java 中消失了

java - 如何为用户输入(单词)分配一个要在代码中使用的整数值

c# - 生成所有递增的数字

java - 有没有办法刷新 Android 中 SQLite 数据库文件的更改?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 我在扫描字符串时遇到问题