2013-03-22 56 views
0

好吧,看起來我遇到了我的主要課程中的一些問題。它在第二次運行時打破了我的循環,並顯示和錯誤。它說我的掃描儀從菜單讀取用戶選擇會產生錯誤?這是怎麼回事,它在第一個循環中工作,但由於某種原因它不能再次運行。我在循環中收到錯誤。線程「main」java.util.NoSuchElementException中的異常。錯誤是我的掃描儀?

"action = new Scanner(System.in).nextInt();" 

正在產生錯誤。有誰知道爲什麼會發生這種情況,因爲在用戶選擇菜單選項時讀取用戶放入的整數非常重要。

import java.util.Scanner; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.lang.String; 
import java.lang.System; 

public class MainActions { 

    public static void main(String args[]) throws IOException { 

     int action = 0; 

     while (action != 6) { 

      Contact.menu(); 

      new Scanner(System.in); 

      action = new Scanner(System.in).nextInt(); 

      if (action <= 0 || action > 6) { 
       System.out.println("Invalid selection. "); 
      } 

      switch (action) { 
      case 1: 
       MainActions.addContactInfo(); 
       break; 

      case 2: 
       break; 

      case 3: 
       break; 

      case 4: 
       break; 

      case 5: 
       break; 
      } 
     } 
    } 

    public static void addContactInfo() { 

     Contact contact; 
     contact = new Contact(); 

     Scanner reader = new Scanner(System.in); 
     System.out.println("Enter Contact Last Name:"); 
     String lastname = reader.nextLine(); 
     contact.setLastName(lastname); 
     System.out.println("Enter Contact First Name: "); 
     contact.setFirstName(reader.nextLine()); 
     System.out.println("Enter Contact Street Address: "); 
     contact.setHouseAddress(reader.nextLine()); 
     System.out.println("Enter Contact City: "); 
     contact.setCity(reader.nextLine()); 
     System.out.println("Enter Contact Zip Code: "); 
     contact.setZip(reader.nextLine()); 
     System.out.println("Enter Contact Email: "); 
     contact.setEmail(reader.nextLine()); 
     System.out.println("Enter Contact Phone Number: "); 
     contact.setPhone(reader.nextLine()); 
     System.out.println("Enter Contact Notes: "); 
     contact.setNotes(reader.nextLine()); 

     if (lastname.trim().equals("")) { 
      System.out.println("\nLast name was blank, contact not saved."); 
      System.exit(0); 
     } else { 
      ContactList list; 
      list = new ContactList(); 
      list.add(contact); 
      list.save(); 
      Contact c = contact; 
      try (PrintWriter output = new PrintWriter(new FileWriter("contactlist.csv", true))) { 
       output.printf("%s\r\n", c); 
      } catch (Exception e) {} 
     } 
     reader.close(); 
    } 
} 

控制檯:

1. Enter a new person 
2. Print the contact list 
3. Retrieve a person's information by last name 
4. Retrieve a person's information by email address 
5. Retrieve all people who live in a given zip code 
6. Exit 
1 
Enter Contact Last Name: 
asdf 
Enter Contact First Name: 
asdf 
Enter Contact Street Address: 
asdf 
Enter Contact City: 
asdf 
Enter Contact Zip Code: 
asdf 
Enter Contact Email: 
asdf 
Enter Contact Phone Number: 
asdf 
Enter Contact Notes: 
asdf 

Contact information has been saved. 

Exception in thread "main" java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at MainActions.main(MainActions.java:33) 
1. Enter a new person 
2. Print the contact list 
3. Retrieve a person's information by last name 
4. Retrieve a person's information by email address 
5. Retrieve all people who live in a given zip code 
6. Exit 
+0

查看我的答案。它會解決你的問題。 – SudoRahul 2013-03-22 16:01:23

回答

1
new Scanner(System.in); // Remove this. Not needed. 
action = new Scanner(System.in).nextInt(); 

等待起來,這是不是問題。這是實際的問題。

reader.close(); 

這條線你addContactInfo()罪魁禍首。刪除這個和你的代碼將工作。

reader.close()關閉你的Scanner(掃描System.in)在方法中,因爲二者都是掃描System.in

希望這可以解決您的問題。我碰巧找到this question on SO。查看更詳細的解釋。

文件說,這 -

公共無效的close()拋出IOException異常

關閉此輸入流並釋放與此流有關 任何系統資源。關閉的總體合同是關閉 輸入流。關閉的流不能執行輸入操作,並且不能重新打開 。

+0

R J是JAVA的高手!這確實解決了這個問題。謝謝你,先生! – user2188354 2013-03-22 16:06:18

+0

高興地幫助:)不要忘記接受答案:) – SudoRahul 2013-03-22 16:09:10

+0

接受答案。 :) 再次感謝。 – user2188354 2013-03-22 16:35:14

2

更換

new Scanner(System.in); 

action = new Scanner(System.in).nextInt(); 

隨着

Scanner scan = new Scanner(System.in); 

action = scan.nextInt(); 
+0

不,同樣的錯誤被拋出。錯誤行32「action = scan.nextInt();」 – user2188354 2013-03-22 15:45:39

相關問題