-1

My code is running fine at the beginning, but in the while loop, it won't prompt the user for the next input, and is just responding with no such element exception. I have tried fixing it for at least an hour, and have had no luck. Any tips on what might be wrong with it?

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



    }
}

results in:

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)  

EDIT: Here are the other classes of the program:

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.");
    }
    }

}

Item Class:

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 Answers2

0

You are calling Scanner.next() without first calling Scanner.hasNext().

Scanner.next is throwing the exception because it's reached the end of file. You should end your loop when Scanner.hasNext() returns false (check at the start using a while loop.)

EDIT: You have closed a locally-declared Scanner in method addItem(). That has the side-effect of closing the backing channel, i.e. standard input. Therefore, the scanner in your main loop can no longer get any more input - it sees a closed file.

Although it's good general advice to close whatever you open in the same method, in the case of standard input you should only ever close that your application exits.

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • The scanner is not reading a file though, it is supposed to get more user input from the user at that time, but it throws the error instead of asking for the input – CoderPerson Mar 17 '17 at 15:12
  • That's interesting. The stack trace shows that Scanner.next threw a NoSuchElementException. This happens when it reaches the end of the file. This can also be signalled from the console by entering control-Z on Windows or control-D on Linux/MacOS. Are you certain you didn't type one of those control characters? – Klitos Kyriacou Mar 17 '17 at 15:17
  • Yes, and I have tried using Scanner.nextLine or Scanner.nextInt, but the same error occurs. There is no file usage in the program. – CoderPerson Mar 17 '17 at 15:18
  • Ah, that's because you do `input.close()` in `addItem`. That closes the standard input, so the next time you try to get a choice, the standard input is closed and so the scanner cannot get any more input. – Klitos Kyriacou Mar 17 '17 at 15:21
  • That fixed it! Thank you so much! – CoderPerson Mar 17 '17 at 15:24
0

It seems to be a problem with your code using multiple Scanner objects, try to define a single Scanner instance and then use it all over the code (check this answer for more details: How to use multiple Scanner objects on System.in?).

Also, you missed the equals and hashCode overriding for the Item class, mandatory when calling indexOf if you want the correct object to be retrieved:

So I will post here a version that is working fine for me:

Store class:

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 class:

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 class:

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


    }
}
Community
  • 1
  • 1
aUserHimself
  • 1,589
  • 2
  • 17
  • 26