-1

When I'm using Scanner to get character in java, compiler throws:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at ShoppingCartManager.getChar(ShoppingCartManager.java:37)
    at ShoppingCartManager.printMenu(ShoppingCartManager.java:56)
    at ShoppingCartManager.main(ShoppingCartManager.java:30)

Here's the part of code where exception is thrown

private static final Scanner scan = new Scanner(System.in);

public static void main(String[] args) {
    Scanner scnr = new Scanner (System.in);
    System.out.println("Enter Customer's Name:");
    String name = scnr.nextLine();
    System.out.println("Enter Today's Date:");
    String date = scnr.nextLine();
    System.out.println();

    ShoppingCart cart = new ShoppingCart(name, date);

    System.out.println("Customer Name: " + name);
    System.out.println("Today's Date: " + date + "\n");

    printMenu(cart);
    scan.close();

}

private static char getChar () {
    char option;

    while (true) {
        try {
           option = scan.next().charAt(0);
           break;
        } catch (Exception exc) {
           continue;
        }
    }
    return option;
}

public static void printMenu(ShoppingCart cart) {

    while(true) {
        System.out.println("MENU");
        System.out.println("g - Add Guitar to cart");
        System.out.println("c - Add Candy Bar to cart");
        System.out.println("s - Add Shoes to cart");
        System.out.println("d - Remove item from cart");
        System.out.println("n - Change item quantity");
        System.out.println("i - Output items' descriptions");
        System.out.println("o - Output shopping cart");
        System.out.println("q - Quit" + "\n");
        System.out.println("Choose an option:");

        char option = getChar();

        if (option == 'q') {
            return;

        } else if (option == 'g') {
            File guitar;
            Scanner iss;
            try {
                guitar = new File("Guitar.txt");
                iss = new Scanner(guitar);
                ItemToPurchase item = new ItemToPurchase();
                item.setName(iss.nextLine());
                item.setDescription(iss.nextLine());
                item.setPrice(Integer.parseInt(iss.nextLine()));
                item.setQuantity(Integer.parseInt(iss.nextLine()));
                cart.addItem(item);
                System.out.println("ADDED GUITAR TO CART");
                iss.close();
            } catch (IOException excp) {
                System.out.print(excp);
            }

        } else if (option == 'c') {
            File candyBar;
            Scanner iss;
            try {
                candyBar = new File("Candy_Bar.txt");
                iss = new Scanner(candyBar);
                ItemToPurchase item = new ItemToPurchase();
                item.setName(iss.nextLine());
                item.setDescription(iss.nextLine());
                item.setPrice(Integer.parseInt(iss.nextLine()));
                item.setQuantity(Integer.parseInt(iss.nextLine()));
                cart.addItem(item);
                System.out.println("ADDED CANDY BAR TO CART");
                iss.close();
            } catch (IOException excp) {
                System.out.print(excp);
            }

        } else if (option == 's') {
            File shoes;
            Scanner iss;
            try {
                shoes = new File("Shoes.txt");
                iss = new Scanner(shoes);
                ItemToPurchase item = new ItemToPurchase();
                item.setName(iss.nextLine());
                item.setDescription(iss.nextLine());
                item.setPrice(Integer.parseInt(iss.nextLine()));
                item.setQuantity(Integer.parseInt(iss.nextLine()));
                cart.addItem(item);
                System.out.println("ADDED SHOES TO CART");
                iss.close();
            } catch (IOException excp) {
                System.out.print(excp);
            }

        } else if (option == 'd') {

            System.out.println("REMOVE ITEM FROM CART");
            System.out.println("Enter name of item to remove");
            String item = scan.nextLine();
            cart.removeItem(item);

        } else if (option == 'n') {

            System.out.println("CHANGE ITEM QUANTITY");
            System.out.println("Enter the item name");
            String name = scan.nextLine();
            System.out.println("Enter the new quantity");
            int newQuantity = scan.nextInt();

            for (int i = 0; i < cart.cartItems.size(); i++) {
                if (cart.cartItems.get(i).getName().equals(name)) {
                    cart.cartItems.get(i).setQuantity(newQuantity);
                }

            }

        } else if (option == 'i') {

            System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
            cart.printDescriptions();

        } else if (option == 'o') {

            System.out.println("OUTPUT SHOPPING CART");
            cart.printTotal();
        } else {
            continue;
        }

    }
}

}

what should I do?

  • 1
    [What did your debugger tell you](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – achAmháin Oct 22 '18 at 14:46
  • when just entering characters , it works fine; but when I submit is on Zybook, that's when it throws an exception. zybook checks like ths: it enters characters as seen x w q that's when exception is thrown – Giorgi Meladze Oct 22 '18 at 14:50
  • I even tried to call just char option = scan.next().charAt(0) but is still throws same exception how can I 'add' Line? – Giorgi Meladze Oct 22 '18 at 14:53
  • Also be careful doing this all over the place: `iss.close();` - read [this](https://stackoverflow.com/questions/42925771/java-util-nosuchelementexception-error-when-closing-the-scanner) - this is likely your problem. – achAmháin Oct 22 '18 at 15:03

2 Answers2

0

You need also to check if there is a next line when you are using scanner, you can use hasNextLine() to perform this check.

Stephen Ostermiller
  • 18,578
  • 8
  • 74
  • 95
0

with Scanner you need to check if there is a next line with hasNext() so the loop becomes

while(sc.hasNext()){
str=sc.next().charAt(0);
//...

}

Something like this should work. java.util.NoSuchElementException: No line found

  • i tried like that also, but it throw Program generated too much output. Output restricted to 50000 characters. Check program for any unterminated loops generating output. – Giorgi Meladze Oct 22 '18 at 15:13