我怎样才能在菜单循环中解决这个扫描仪问题?

How can i solve this scanner problem in menu loop?

我制作了一个使用 MySQL 进行一些操作的应用程序,为了让用户可以选择他可以执行的操作,我创建了一个具有多种选择的菜单循环。 这里的代码:

public void MainMenu() throws DatabaseConnectionException, SQLException, ParseException, AuthorPublisherNotFoundException {
    ConnectionMySQL cd = new ConnectionMySQL();
    cd.initConnection();

    BookDao BookDao = new BookDao(cd.getConnection());
    AuthorDao AuthorDao = new AuthorDao(cd.getConnection());
    PublisherDao PublisherDao = new PublisherDao(cd.getConnection());
    DeleteAuthor deleteAuthor = new DeleteAuthor(cd.getConnection());
    DeletePublisher deletePublisher = new DeletePublisher(cd.getConnection());
    CheckAuthorFromBook checkA = new CheckAuthorFromBook(cd.getConnection());
    CheckPublisherFromBook checkP = new CheckPublisherFromBook(cd.getConnection());
    GetAuthorName getAuthorName = new GetAuthorName(cd.getConnection());
    GetPublisherName getPublisherName = new GetPublisherName(cd.getConnection());

    System.out.println("\n***BOOKSHOP APPLICATION***\n");

    int choice;
    while (true) {

        System.out.println("\nPOSSIBILE ACTION:");
        System.out.println("1" + " Insert new author");
        System.out.println("2" + " Delete an author");
        System.out.println("3" + " Browse authors");
        System.out.println("4" + " Insert a new publisher");
        System.out.println("5" + " Delete a publishern");
        System.out.println("6" + " Browse publishers");
        System.out.println("7" + " Insert a new book");
        System.out.println("8" + " Delete a book");
        System.out.println("9" + " Browse books");
        System.out.println("10" + " Update quantity of a book(+/-)");
        System.out.println("0" + " Terminate program");

        System.out.println("\nWhat you want to do?");
        Scanner inputA = new Scanner(System.in);
        choice = inputA.nextInt();

        switch (choice) {
            case 1:
                String fName = null;
                String lName;
                String biography;
                System.out.println("Insert the name, the last name and the biography of the author");
                Scanner input1 = new Scanner(System.in);
                System.out.println("Name:");
                fName = input1.next();
                System.out.println("Surname:");
                lName = input1.next();
                System.out.println("Biography:");
                biography = input1.next();
                AuthorDao.insertAuthor(fName, lName, biography);
                input1.close();
                break;
            case 2:
                int id;
                System.out.println("Insert the identifier of the author (drop the author will drop even his books)");
                Scanner input2 = new Scanner(System.in);
                System.out.println("Identifier:");
                id = input2.nextInt();
                deleteAuthor.deleteAutCascade(id);
                input2.close();
                break;
            case 3:
                System.out.println("Authors list:");
                List<Author> listaAuthor = AuthorDao.getAllAuthor();
                for (Author author : listaAuthor) {
                    System.out.println("Id author: " + author.getAuthor_id() + "   Name: " + author.getFname() + "   Surname: " + author.getLname());
                }
                break;
            case 4:
                String name = null;
                String location = null;
                System.out.println("Insert the name and the location of the new publisher");
                Scanner input3 = new Scanner(System.in);
                System.out.println("Name:");
                name = input3.next();
                System.out.println("Location:");
                location = input3.next();
                PublisherDao.insertPublisher(name, location);
                input3.close();
                break;
            case 5:
                int idPub;
                System.out.println("Insert the identifier of the publisher (drop the publisher will drop even his books)");
                Scanner input4 = new Scanner(System.in);
                System.out.println("Identifier:");
                idPub = input4.nextInt();
                deletePublisher.deletePubCascade(idPub);
                input4.close();
                break;
            case 6:
                System.out.println("Publisher list:");
                List<Publisher> listaPublisher = PublisherDao.getAllPublisher();
                for (Publisher publisher : listaPublisher) {
                    System.out.println("Id publisher: " + publisher.getPublisher_id() + "   Name: " + publisher.getName() + "   Location: " + publisher.getLocation());
                }
                break;
            case 7:
                String title;
                Float price = null;
                String category;
                int pubblicationYear;
                int numPages;
                Integer idPublisher;
                int quantity;
                Integer idAuthor;
                System.out.println("Insert the features of the new book");
                Scanner input = new Scanner(System.in);
                System.out.println("Title:");
                title = input.nextLine();
                System.out.println("Price:");
                price = input.nextFloat();
                System.out.println("Category:");
                category = input.nextLine();
                System.out.println("Year of publication:");
                pubblicationYear = input.nextInt();
                System.out.println("Number of pages:");
                numPages = input.nextInt();
                System.out.println("Publisher:");
                idPublisher = input.nextInt();
                System.out.println("Quantity");
                quantity = input.nextInt();
                System.out.println("Author");
                idAuthor = input.nextInt();

                try {
                    if (checkA.checkAuthor(idAuthor)) {
                        if (checkP.checkPublisher(idPublisher)) {
                            BookDao.insertBook(title, price, category, pubblicationYear, numPages, idPublisher, quantity, idAuthor);
                            input.close();
                        } else {

                        }
                    } else {

                    }
                } catch (Exception e) {
                    throw new AuthorPublisherNotFoundException("Please add the publisher or the author in respective section before inserting a book with them!");
                }
                input.close();
                break;
            case 8:
                int idB;
                System.out.println("Insert the identifier of the book that you want to drop: ");
                Scanner input5 = new Scanner(System.in);
                System.out.println("Identifier:");
                idB = input5.nextInt();
                BookDao.deleteBook(idB);
                input5.close();
            case 9:
                System.out.println("Books list:");
                List<Book> listaBook = BookDao.getAllBooks();
                for (Book book : listaBook) {
                    System.out.println("Id Book: " + book.getBook_id() + "   Title: " + book.getTitle() + "   Price: " + book.getPrice()
                            + "  Category:  " + book.getCategory() + "  Year of publication: " + book.getYear() + "  Number of pages:  "
                            + book.getNumPage() + "  Publisher: " + getPublisherName.publisherName(book.getPublisher_id()) + "  Quantity:  "
                            + book.getQuantity() + "  Author: " + getAuthorName.authorName(book.getAuthor_id()));
                }
                break;
            case 10:
                int qty; int idBook;
                System.out.println("Insert the ID of the book that you want to update with the new quantity in stock:");
                Scanner input6 = new Scanner(System.in);
                System.out.println("Identifier:");
                idBook = input6.nextInt();
                System.out.println("Qty:");
                qty=input6.nextInt();
                BookDao.modifyQuantity(idBook,qty);
                input6.close();
                break;
            case 0:
                System.out.println("Exiting program...");
                cd.closeConnection();
                System.exit(0);
                break;
            default:
                System.out.println("This is not a valid menu option... Please try again!");
                break;
        }
    }
}

我在用户放置菜单选项的第一个 InputA 上遇到了一些问题。特别是我有这种类型的错误:

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 utilities.menu.MainMenu(menu.java:58)
at Main.main(Main.java:11)

肯定是扫描仪有问题,但是是什么原因造成的? 提前谢谢你。

如评论中所述:您需要使用 one Scanner,您不应该写 while(true) 因为它会使更多代码无法访问,您需要关闭那个 Scanner (inputA) 最后。

我写了一个你的函数的简化版本:

public void MainMenu() {

    System.out.println("\n***BOOKSHOP APPLICATION***\n");

    int choice = 1;
    Scanner inputA = new Scanner(System.in);
    while (choice != 0) {

        System.out.println("\nPOSSIBILE ACTION:");
        System.out.println("1" + " Insert new author");
        System.out.println("2" + " Delete an author");
        System.out.println("3" + " Browse authors");
        System.out.println("4" + " Insert a new publisher");
        System.out.println("5" + " Delete a publishern");
        System.out.println("6" + " Browse publishers");
        System.out.println("7" + " Insert a new book");
        System.out.println("8" + " Delete a book");
        System.out.println("9" + " Browse books");
        System.out.println("10" + " Update quantity of a book(+/-)");
        System.out.println("0" + " Terminate program");

        System.out.println("\nWhat you want to do?");

        choice = inputA.nextInt();

        System.out.println();
        switch (choice) {
        case 1:
            String fName = null;
            String lName;
            String biography;
            System.out.println("Insert the name, the last name and the biography of the author");
            System.out.println("Name:");
            fName = inputA.next();
            System.out.println("Surname:");
            lName = inputA.next();
            System.out.println("Biography:");
            biography = inputA.next();
            break;
        default:
            System.out.println("This is not a valid menu option... Please try again!");
            break;
        }
    }

    inputA.close();
}