2017-03-17 129 views
-1

我的代碼在開始時運行良好,但在while循環中,它不會提示用戶輸入下一個輸入,並且只是響應而沒有此類元素異常。我試圖修復它至少一個小時,並沒有運氣。任何提示可能是錯誤的嗎?NoSuchElementException第一次迭代後在while循環中中斷?

 public class Inventory { 
     public static void main(String[] args) { 
     Store store = new Store(); 
     String itemName; 

     System.out.println("Welcome to the Inventory Application. You can add an item, display the stock for an item, or discontinue an item."); 
     boolean condition = true; 
     Scanner s = new Scanner(System.in); 
     do{ 
      System.out.println("Please select an option and type the option number." + "\n 0. Quit \n 1. Add an item \n 2. Display stock for an item \n 3. Discontinue an item"); 
      String choice = s.next(); 

      if(choice.equals("0")){ 
       System.out.println("Thank you for using the Sports Inventory Application. Good bye."); 
       condition = false; 
       System.exit(0); 
      } 
      else if(choice.equals("1")){ 
       store.addItem(); 
      } 
      else if(choice.equals("2")){ 
       System.out.println("Item Name: "); 
       itemName = s.nextLine(); 
       store.displayItem(itemName); 
      } 
      else if(choice.equals("3")){ 
       System.out.println("Item Name: "); 
       itemName = s.nextLine(); 
       store.deleteItem(itemName); 
      } 
     } 
     while(condition == true); 



    } 
} 

結果:

Welcome to the Inventory Application. You can add an item, display the stock for an item, or discontinue an item. 
Please select an option and type the option number. 
0. Quit 
1. Add an item 
2. Display stock for an item 
3. Discontinue an item 
1 
Item Name: 
test 
Item Amount: 
120 
Item added. Information: test 
Current amount in inventory is: 120 
Please select an option and type the option number. 
0. Quit 
1. Add an item 
2. Display stock for an item 
3. Discontinue an item 
java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Scanner.java:862) 
    at java.util.Scanner.next(Scanner.java:1371) 
    at Inventory.main(Inventory.java:24) 

編輯:這裏是其他類的程序:

import java.util.ArrayList; 

import java.util.Scanner; 

public class Store { 
    private ArrayList<Item> inventory; 

    public Store(){ 
     inventory = new ArrayList<Item>(); 
    } 

public void addItem(){ 
    Item newItem; 
    int itemAmount; 
    String itemName; 
    Scanner input = new Scanner(System.in); 

    System.out.println("Item Name: "); 
    itemName = input.nextLine(); 
    System.out.println("Item Amount: "); 
    itemAmount = input.nextInt(); 

    newItem = new Item(itemName, itemAmount); 
    inventory.add(newItem); 

    System.out.println("Item added. Information: " + inventory.get(0)); 
    input.close(); 
} 

public void deleteItem(String itemName){ 
    int itemIndex; 
    Item itemToDelete; 

    itemToDelete = new Item(itemName); 
    itemIndex = inventory.indexOf(itemToDelete); 
    if(itemIndex > -1){ 
     inventory.remove(itemIndex); 
    } 
    else{ 
     System.out.println("Item does not exist."); 
    } 
} 
public void displayItem(String itemName){ 
    int itemIndex; 
    Item itemToDisplay, item; 

    itemToDisplay = new Item(itemName); 
    itemIndex = inventory.indexOf(itemToDisplay); 
    if (itemIndex > -1){ 
     item = inventory.get(itemIndex); 
     System.out.println(item); 
    } 
    else{ 
     System.out.println("Item does not exist."); 
    } 
    } 

} 

項目類:

public class Item { 
    private int itemAmount; 
    private String itemName; 

    public Item(String name, int amount){ 
     this.itemName = name; 
     this.itemAmount = amount; 
    } 
    public Item(String name){ 
     itemAmount = 0; 
     this.itemName = name; 
    } 

    public int getItemAmount(){ 
     return itemAmount; 
    } 
    public String getItemName(){ 
     return itemName; 
    } 
    public String getItem(){ 
     return itemName + itemAmount; 
    } 
    @Override 
    public String toString(){ 
     String itemString; 
     itemString = this.itemName + "\n"; 
     itemString += "Current amount in inventory is: " + this.itemAmount; 
     return itemString; 
    } 
} 
+2

任何堆棧跟蹤? –

+0

java.util.NoSuchElementException \t在java.util.Scanner.throwFor(Scanner.java:862) \t在java.util.Scanner.next(Scanner.java:1371) \t在Inventory.main(Inventory.java :24) – CoderPerson

+0

@SteveSmith我把它添加到文章 – CoderPerson

回答

0

您無需調用Scanner.next()首先呼叫Scanner.hasNext()

Scanner.next正在拋出異常,因爲它已到達文件末尾。你應該結束循環時Scanner.hasNext()返回false(檢查在使用while循環的開始。)

編輯: 你已經在方法addItem()關閉本地聲明的掃描儀。這具有關閉支持通道的副作用,即標準輸入。因此,主循環中的掃描器不能再獲得任何輸入 - 它會看到一個關閉的文件。

雖然在同樣的方法中關閉您打開的任何一般建議是很好的,但在標準輸入的情況下,您應該只關閉應用程序退出。

+0

儘管掃描儀並未讀取文件,但它應該在當時從用戶那裏獲得更多的用戶輸入,但它會拋出錯誤而不是要求輸入 – CoderPerson

+0

這很有趣。堆棧跟蹤顯示Scanner.next引發了NoSuchElementException。當它到達文件的末尾時會發生這種情況。通過在Windows上輸入control-Z或在Linux/MacOS上輸入control-D,也可以從控制檯發出信號。你確定你沒有輸入其中一個控制字符? –

+0

是的,我嘗試使用Scanner.nextLine或Scanner.nextInt,但發生同樣的錯誤。程序中沒有文件用法。 – CoderPerson

0

這似乎是使用多個代碼Scanner對象問題, 試圖定義一個Scanner實例,然後使用它在所有的代碼(檢查這個答案詳細信息:How to use multiple Scanner objects on System.in?)。

此外,在調用indexOf時錯過了equalshashCode壓倒一切的Item類,強制性的,如果你想被檢索正確的對象:

因此,我將在這裏發佈的是對我工作的罰款版本:

Store類:

public class Store { 
    private ArrayList<Item> inventory; 

    public Store(){ 
     inventory = new ArrayList<>(); 
    } 

    public void addItem(Scanner input){ 
     Item newItem; 
     int itemAmount; 
     String itemName; 

     System.out.println("Item Name: "); 
     itemName = input.nextLine(); 
     System.out.println("Item Amount: "); 
     itemAmount = Integer.parseInt(input.nextLine()); 

     newItem = new Item(itemName, itemAmount); 
     inventory.add(newItem); 

     System.out.println("Item added. Information: " + inventory.get(0)); 
    } 

    public void deleteItem(String itemName){ 
     int itemIndex; 
     Item itemToDelete; 

     itemToDelete = new Item(itemName); 
     itemIndex = inventory.indexOf(itemToDelete); 
     if(itemIndex > -1){ 
      inventory.remove(itemIndex); 
     } 
     else{ 
      System.out.println("Item does not exist."); 
     } 
    } 
    public void displayItem(String itemName){ 
     int itemIndex; 
     Item itemToDisplay, item; 

     itemToDisplay = new Item(itemName); 
     itemIndex = inventory.indexOf(itemToDisplay); 
     if (itemIndex > -1){ 
      item = inventory.get(itemIndex); 
      System.out.println(item); 
     } 
     else{ 
      System.out.println("Item does not exist."); 
     } 
    } 

} 

Item類:

public class Item { 
    private int itemAmount; 
    private String itemName; 

    public Item(String name, int amount){ 
     this.itemName = name; 
     this.itemAmount = amount; 
    } 
    public Item(String name){ 
     itemAmount = 0; 
     this.itemName = name; 
    } 

    public int getItemAmount(){ 
     return itemAmount; 
    } 
    public String getItemName(){ 
     return itemName; 
    } 
    public String getItem(){ 
     return itemName + itemAmount; 
    } 

    @Override 
    public String toString(){ 
     String itemString; 
     itemString = this.itemName + "\n"; 
     itemString += "Current amount in inventory is: " + this.itemAmount; 
     return itemString; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (!(o instanceof Item)) return false; 

     Item item = (Item) o; 

     return itemName.equals(item.itemName); 

    } 

    @Override 
    public int hashCode() { 
     return itemName.hashCode(); 
    } 

Inventory類:

public class Inventory { 
    public static void main(String[] args) { 
     Store store = new Store(); 
     String itemName; 

     System.out.println("Welcome to the Inventory Application. You can add an item, display the stock for an item, or discontinue an item."); 
     boolean condition = true; 
     Scanner s = new Scanner(System.in); 
     do { 
      System.out.println("Please select an option and type the option number." + "\n 0. Quit \n 1. Add an item \n 2. Display stock for an item \n 3. Discontinue an item"); 
      String choice = s.nextLine(); 

      if (choice.equals("0")) { 
       System.out.println("Thank you for using the Sports Inventory Application. Good bye."); 
       condition = false; 
       System.exit(0); 
      } else if (choice.equals("1")) { 
       store.addItem(s); 
      } else if (choice.equals("2")) { 
       System.out.println("Item Name: "); 
       itemName = s.nextLine(); 
       store.displayItem(itemName); 
      } else if (choice.equals("3")) { 
       System.out.println("Item Name: "); 
       itemName = s.nextLine(); 
       store.deleteItem(itemName); 
      } 
     } 
     while (condition == true); 


    } 
} 
相關問題