当前位置: 首页 > 知识库问答 >
问题:

如何在菜单循环中解决此扫描仪问题?

公冶渝
2023-03-14

我做了一个应用程序,用MySQL做一些操作,并给用户选择他可以做的操作的可能性,我创建了一个有几个选择的men循环。下面是代码:

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

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

共有1个答案

岳承悦
2023-03-14

正如注释中的statet:您需要使用一个扫描器,您不应该编写while(true),因为它会使更多的代码无法访问,并且最终需要关闭那个扫描器(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();
}
 类似资料:
  • 嗨,我正在编写一个程序,它使用Scanner从用户那里获取输入,然后使用布尔方法检查输入的长度是否不超过六个字符。问题是,如果长度小于六个,我使用了while循环来不断请求输入;但是在第一次错误输入后,如果你插入一串六个字母或更长的字符串,循环仍然会继续。这是程序: 谢谢

  • 我开发了一个字符排序器,我想每次字符串排序后提示用户输入一个新的字符串。我遇到的问题是扫描仪一直在扫描用户的第一个输入。如果我使用scanner.next(),它不会计算输入末尾的空白,这不是解决方案。 这是while循环的开始。一旦代码完成,它将再次从“inputtext”开始。

  • 我正在尝试编写使用Scanner类从用户那里获取输入的代码。但是hasNext()、hasNextInt()和hasNextLine()每次都会运行无限循环。

  • 问题内容: 我必须在 while 循环中显示Scanner输入:用户必须插入输入,直到他写下“ quit”为止。因此,我必须验证每个输入以检查他是否写出“ quit”。我怎样才能做到这一点? 这行不通。如何验证每个用户的输入? 问题答案: 问题是nextLine() “使该扫描程序前进到当前行之外”。因此,当您调用该条件并且不保存返回值时,您将丢失用户输入的那一行。对第3行的调用返回另一行。 您可

  • 问题内容: 如何将扫描仪的定界符设置为;或换行? 我试过: 但这不起作用。 问题答案: 通常,在模式中,您需要将翻倍。 所以,尝试 要么 编辑 :如果是问题,则可能要尝试以下操作: 它匹配的一个或多个,和。 注意 :我还没有尝试过这些。

  • 问题内容: 我正在尝试读取具有多个单词的字符串,即。洛杉矶或纽约市。如果有两个词并将它们分割在变量之间,则使用scanner.next()进行“出发”和“到达”只会读取第一个。nextLine()也不是很幸运。这是我的代码: 我知道这很简单,但我还没有弄清楚。 这是上面的代码的输入/输出: 输入航班号:29 输入出发城市:(立即跳至下一行) 输入到达城市: -—我真正想要的是---- 输入航班号: