Java运行时异常-“ java.util.NoSuchElementException”

时间:2019-03-03 08:04:36

标签: java

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:1478)
at ShoppingCartManager.printMenu(ShoppingCartManager.java:24)
at ShoppingCartManager.main(ShoppingCartManager.java:15)

我的代码问题似乎在我的代码的第24行和第15行中,我花了很多时间试图对其进行修复,请提供帮助。

  public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  System.out.println("Enter Customer's Name:");
  String customerName = scnr.nextLine();
  System.out.println("Enter Today's Date:");
  String currentDate = scnr.nextLine();
  ShoppingCart shopCart = new ShoppingCart(customerName, currentDate);
  System.out.println();
  System.out.println("Customer Name: "+ shopCart.getCustomerName());
  System.out.println("Today's Date: "+ currentDate);
  System.out.println("");
  printMenu(shopCart); // LINE 15

  }

  public static void printMenu(ShoppingCart shopCart) {
  while(true) {
     System.out.println("MENU\na - Add item to cart\nd - Remove item from 
     cart\nc - Change item quantity\ni - Output items' descriptions\no -
     Output  shopping cart\nq - Quit\n\nChoose an option:");
     Scanner scnr = new Scanner(System.in);
     char ch = scnr.next().charAt(0); // LINE 24
     scnr.nextLine();
     if(ch == 'a' || ch == 'A' ) {
        System.out.println("ADD ITEM TO CART");
        System.out.println("Enter Item Name: ");
        String name = scnr.nextLine();

        System.out.println("Enter Item Description: ");
        String itemDescritpion = scnr.nextLine();
        System.out.println("Enter Item Price: ");
        int itemPrice = scnr.nextInt();
        System.out.println("Enter Item Quantity: ");
        int quantity = scnr.nextInt();
        scnr.nextLine();
        ItemToPurchase item = new ItemToPurchase(name, itemDescritpion,itemPrice, quantity);
        shopCart.addItem(item);
     }

这是一个OnlineShoppingCart问题,它可以在eclipse上运行,但是在我要提交到的在线网站上不起作用。我尝试转换char ch = scnr.next()。charAt(0); scnr.nextLine();字符串a = sncr.nextLine(); char ch = a.charAt(0);但这也不起作用,而且一开始仍然给我同样的错误。

0 个答案:

没有答案