這個問題在這里已經有了答案: java.util.NoSuchElementException - 掃描儀讀取用戶輸入 (4 個回答) 4 小時前關閉。
我制作了一個應用程式,可以使用 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)
掃描儀肯定有問題,但是是什么原因造成的?先感謝您。
uj5u.com熱心網友回復:
正如評論中的宣告:你需要使用一個掃描器,你不應該寫 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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334815.html
上一篇:無法更新輸出
下一篇:無法將大量Prime列印到控制臺
