2014-04-20 172 views
0

我正在做一個程序,爲每個員工初始化值,然後它最終顯示。掃描儀關閉後,我在第四次循環後遇到掃描儀問題,我得到錯誤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

你在哪裏宣佈掃描儀? 我的猜測是你正在關閉掃描儀的某處並試圖訪問它。 – Bharat

回答

1

所有的使用將拋出IllegalStateException如果掃描儀是關閉的,當您嘗試使用他們Scanner方法。在您的代碼中的某處,您可能有keyboard.close()。刪除這一行,或將其移動到程序的最後,並且您是黃金版。從Java文檔:

試圖在掃描程序關閉後執行搜索操作將導致IllegalStateException。