java.util.NoSuchElementException问题:找不到行

时间:2018-10-22 14:44:50

标签: java exception

当我使用Scanner获取Java中的字符时,编译器将抛出:

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)

这是引发异常的代码部分

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

    }
}

}

我该怎么办?

2 个答案:

答案 0 :(得分:0)

在使用扫描仪时,还需要检查是否还有下一行,可以使用hasNextLine()进行此检查。

答案 1 :(得分:0)

使用Scanner,您需要检查hasNext()是否存在下一行 这样循环就变成了

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

}

类似的事情应该起作用。 java.util.NoSuchElementException: No line found