Home > Mobile >  How can i solve this scanner problem in menu loop?
How can i solve this scanner problem in menu loop?

Time:10-24

I made an application that makes some operations with MySQL and to give to the user the possibility of choiche which operation he can do, I create a menù loop with several choices. Here the code:

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

I have some problems with the first InputA where the user put the menu choice. In particular I have this type of error:

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)

For sure there is a problem with the scanner but is a thing due to what? Thank you in advance.

CodePudding user response:

As statet in the comments: you need to use one Scanner, you should not write while(true) as it makes further code unreachable and you need to close that one Scanner (inputA) in the end.

I wrote a simplified version of your function that works:

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();
}
  • Related