-2

I have looked everyone where a reasonable explanation as to how I can fix the problem. Can someone look at my code and give me some suggestions.

Here is my code:

import java.util.Scanner;

public class ShoppingCartManager {

    public static void printMenu(ShoppingCart cart){
        Scanner read = new Scanner(System.in);
        String input = " ";

      /*  String itemName = "none";
        String itemDescription = "none";
        int itemPrice = 0;
        int itemQuantity = 0;*/

        System.out.println("MENU");
        System.out.println("a - Add item to cart");
        System.out.println("d - Remove item from cart");
        System.out.println("c - Change item quantity");
        System.out.println("i - Output items' descriptions");
        System.out.println("o - Output shopping cart");
        System.out.println("q - Quit");

        //ItemToPurchase shopCart = new ItemToPurchase();

        input = read.next();
        read.close();



        switch (input){

            case "q" : System.out.println("Quit");
            break;
        }

    }

    public static void main(String[] args){
        Scanner read = new Scanner(System.in);
        String customerName = "none";
        String todaysDate = "none";

        System.out.println("Enter the Customer's name: ");
        customerName = read.nextLine();
        System.out.println("Enter Today's Date: ");
        todaysDate = read.nextLine();
        ShoppingCart cart = new ShoppingCart(customerName, todaysDate);


        System.out.println("Customer Name: " + cart.getCustomerName());
        System.out.println("Today's Date: " + cart.getDate());

        read.close();

        printMenu(cart);



        return;
    }
}

I get error:

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at ShoppingCartManager.printMenu(ShoppingCartManager.java:24)
at ShoppingCartManager.main(ShoppingCartManager.java:54)
Joe C
  • 15,324
  • 8
  • 38
  • 50
Sam
  • 1
  • 1

1 Answers1

0

Your issue is the call to read.close() in your main method. Not only does this close the Scanner, it also closes the System.in stream. Meaning that any time you need it (such as in your printMenu method), it is unavailable.

In your case, the close() call is simply unnecessary, and you can safely remove it.

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • Thank you that did the trick. Now I'm trying to create a menu using switch statements and after an option is executed I am trying to get the menu to print again after the execution, so I have assigned the printMenu method into that option to see if it would execute the menu again and now it gives me the same exception error. – Sam Apr 20 '17 at 22:27
  • If this worked for you, please do not forget to upvote and accept this answer. – Joe C Apr 21 '17 at 05:10
  • I did, but since I've only been a member since yesterday it wont reflect on the votes. Your help is very much appreciated. – Sam Apr 21 '17 at 15:35