NoSuchElementException在第一次迭代后中断while循环?

时间:2017-03-17 14:50:51

标签: java arraylist 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 :(得分:0)

您在没有先致电Scanner.next()的情况下致电Scanner.hasNext()

Scanner.next抛出异常,因为它已到达文件末尾。当Scanner.hasNext()返回false时,您应该结束循环(使用while循环在开始时检查。)

编辑: 您已在方法addItem()中关闭了本地声明的扫描程序。这具有关闭后备通道的副作用,即标准输入。因此,主循环中的扫描程序无法再获得任何输入 - 它会看到一个已关闭的文件。

尽管通过相同的方法关闭您打开的任何内容是一个很好的一般建议,但在标准输入的情况下,您应该只关闭您的应用程序退出。

答案 1 :(得分:0)

使用多个Scanner对象的代码似乎存在问题, 尝试定义单个Scanner实例,然后在代码中使用它(查看此答案以获取更多详细信息:How to use multiple Scanner objects on System.in?)。

此外,您错过了equalshashCode覆盖Item类,如果您想要检索正确的对象,则必须在调用indexOf时强制执行:

所以我会在这里发布一个适合我的版本:

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


    }
}