0

this is my first JAVA program about Vending Machine and I have been struggling with passing input Scanner.

package vendingmachine;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class VendingMachine {
    static Money money= new Money( 50, 0);
    private static List<Inventory> listItems = new ArrayList<>();
    private static List<Inventory> listCoins = new ArrayList<>();
    public VendingMachine() {
        listItems.add(new Inventory( "SNACK", 1.25, 10 ));
        listItems.add(new Inventory( "COCA", 3.0, 15));
        listItems.add(new Inventory("GUM", 1.75, 20));
        //coin
        listCoins.add(new Inventory("25", 0.25, 20));
        listCoins.add(new Inventory("100", 1.0, 10));
        listCoins.add(new Inventory("5", 0.05, 30));
        listCoins.add(new Inventory("10", 0.1, 20));
    }
    public static void display() {
        // TODO Auto-generated method stub
        System.out.println("Welcome to Vending Machine: ");
        getmoney(0);
        showItems();
    }
    public static void showItems() {
        System.out.println("Items are shown below");
        for (int i=0; i<listItems.size();i++) {
            System.out.println(listItems.get(i).getName());
        }
        Scanner scan=new Scanner(System.in);
        int num=0;
        System.out.println("Enter a number to get item: ");
        if(scan.hasNextInt()) {
            num=scan.nextInt();
        }
        scan.close();
        if(num<=listItems.size()) {
            getinfo(num);
        } else {
            throw new NoSuchElementException();
        }

        getinfo(num);
    }
    public static void getinfo(int num) {
        if (listItems.get(num).getQuant() > 0 ) {
            System.out.println("You chose " + listItems.get(num).getName());
            System.out.println("The price is $" + listItems.get(num).getPrice());
            System.out.println("There are " + listItems.get(num).getQuant() + " left");
        }
        else {
            System.out.println("Sold Out, Please buy another item");
            showItems();
        }
        if (listItems.get(num).getPrice()<=money.getInsertmoney()) {
            System.out.println("Here your item and balance $" + (money.getInsertmoney()-listItems.get(num).getPrice()));
            listItems.get(num).deduct();
            money.setTotal(money.getTotal()+ money.getInsertmoney());
            System.out.println("---------------------------");
            display();
        }
        else {
            System.out.println("Insufficient money. Please insert more coin");
            getmoney(money.getInsertmoney());
            showItems();
        }
    }
    public static void getmoney(double num) {
        Scanner scan=new Scanner(System.in);
        double insert =0;
        System.out.println("Please insert  only Quarter or One Dollar or Press 0 to Restock: ");
        if (scan.hasNextDouble()) {
            insert=scan.nextDouble();
        }
        scan.close();
        if (insert ==0) {
            restock();
        }
        else {
        money.setInsertmoney(insert+num);;
        System.out.println("Your money: $" + money.getInsertmoney());
        }
    }
    public static void addingItems() {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the name of item you want to add: ");
        String name=scan.nextLine();
        System.out.println("Enter quantity: ");
        int quantity=scan.nextInt();
        System.out.println("Enter price: ");
        double price=scan.nextDouble();
        listItems.add(new Inventory(name, price, quantity));
        System.out.println("Do you want to add more item? Press 0 to add, 1 to collecting money ");
        int num=scan.nextInt();
        scan.close();
        if (num ==0) {
            addingItems();
        }
        else
            collectingmoney();
        getmoney(0);
        showItems();
    }
    public static void collectingmoney() {
        System.out.println("Your total money is: "+ money.getTotal());
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter number for coin value, 5 for Nickel, 10 for Dime, 25 for Quarter, 100 for one Dollar: ");
        String name=scan.nextLine();
        System.out.println("Enter quantity: ");
        int quantity=scan.nextInt();
        System.out.println("Enter price: ");
        double price=scan.nextDouble();
        listItems.add(new Inventory(name, price, quantity));
        System.out.println("Do you want to add more coin? Press 0 to add, 1 to addingItems: ");
        int num=scan.nextInt();
        scan.close();
        if (num ==0) {
            collectingmoney();
        }
        else
            addingItems();
        getmoney(0);
        showItems();
    }
    public static void restock(){
        System.out.println("Input Passwords to add item or to collect or remove money from Storage: ");
        Scanner scan=new Scanner(System.in);
        int num=scan.nextInt();
        scan.close();
        //Password is 6789 for adding item, 9876 for removing money;
        if (num == 6789) {
            addingItems();
        }
        else if (num == 9876){
            collectingmoney();
        }
    }

}

I have been checking my program, and it did not show any problems. However, when I execute it. It showed my this message.

Welcome to Vending Machine: 
Please insert  only Quarter or One Dollar or Press 0 to Restock: 
1.25

Your money: $1.25
Items are shown below
SNACK
COCA
GUM
Enter a number to get item: 
You chose SNACK
The price is $1.25
There are 10 left
Here your item and balance $0.0
---------------------------
Welcome to Vending Machine: 
Please insert  only Quarter or One Dollar or Press 0 to Restock: 
Input Passwords to add item or to collect or remove money from Storage: 
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at chapter1/vendingmachine.VendingMachine.restock(VendingMachine.java:129)
    at chapter1/vendingmachine.VendingMachine.getmoney(VendingMachine.java:78)
    at chapter1/vendingmachine.VendingMachine.display(VendingMachine.java:23)
    at chapter1/vendingmachine.VendingMachine.getinfo(VendingMachine.java:61)
    at chapter1/vendingmachine.VendingMachine.showItems(VendingMachine.java:39)
    at chapter1/vendingmachine.VendingMachine.display(VendingMachine.java:24)
    at chapter1/vendingmachine.main.main(main.java:5)

I couldnot figure out what that means. Can anyone help me fix my program? Thanks yall!!

  • 1
    Remove the `scan.close();` in `restock()` (and **all** of your other methods) - once you close your `Scanner` you also close `System.in` (and then there is no more input). – Elliott Frisch Mar 09 '20 at 23:40
  • You should use only ***one*** `Scanner` based on `System.in` throughout your program, and close it only when you are *completely* done using it (in other words, when your program is about to end). Also, please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). This latter bit is not causing your current problem, but it will be causing your next problem. – Hovercraft Full Of Eels Mar 09 '20 at 23:41

0 Answers0