DarkBlue
DarkBlue

Reputation: 19

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.

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;
    }       

Upvotes: 0

Views: 58

Answers (1)

Azar
Azar

Reputation: 1106

All of the Scanner methods you use will throw an IllegalStateException if the scanner is closed when you attempt to use them. Somewhere in your code, you likely have keyboard.close(). Delete this line, or move it to the end of your program, and you're golden. From the Java documentation:

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

Upvotes: 1

Related Questions