gpt4 book ai didi

在 switch 语句中读取用户输入时出现 java.util.NoSuchElementException

转载 作者:行者123 更新时间:2023-12-02 08:46:38 25 4
gpt4 key购买 nike

我正在为计算机科学类(class)做的这个项目遇到了错误,希望有人能让我深入了解可能发生的情况。代码中发生的情况的一些背景知识:

在下面的代码中,我的程序开始显示 UI 菜单并要求用户输入,并使用 switch 语句根据用户输入运行适当的方法。

    /**
* Start of program
*/
public static void main(String[] args) {

Library library = new Library();

char userChoice; //Stores what menu item the user chose
Scanner userMenuChoiceScanner = new Scanner(System.in);
boolean loop = true;

displayMenu(); //Displays main UI menu
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0)); //Gets user's menu choice

/*Loops UI menu afer user has made menu choice and exits loop if user choses 'q' from menu.*/
while(loop) {

/* Switch descides what menu choice user has made and whether to prin menu UI again, load,
* search, print, or analyze a music catalog, or to quit program.
*/
switch(userChoice) {
case 'm':
System.out.println();
displayMenu();
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0));
break;
case 'p':
System.out.println();

if(library.getBooks().size() != 0) {
printLibrary(library);
} else {
System.out.println("There are no books in the library. Please add a book first.");
}

System.out.print("Please enter a command (press 'm' for Menu):");
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0));
break;
case 'a':
System.out.println();
addBookToLibrary(library);
System.out.print("Please enter a command (press 'm' for Menu):");
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0));
break;
case 'd':

System.out.println();

if(library.getBooks().size() != 0) {
deleteBookFromLibrary(library);
} else {
System.out.println("There are no books in the library. Please add a book first.");
}

System.out.print("Please enter a command (press 'm' for Menu):");
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0));
break;
case 'r':
System.out.println();

if(library.getBooks().size() != 0) {
readBookFromLibrary(library);
} else {
System.out.println("There are no books in the library. Please add a book first.");
}

System.out.print("Please enter a command (press 'm' for Menu):");
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0));
break;
case 'q':
System.out.println();
System.out.println("Thank you! Goodbye!");
loop = false;
break;
default:
System.out.println();
System.out.println("Invalid selection!");
System.out.print("Please enter a command (press 'm' for Menu):");
userChoice = Character.toLowerCase(userMenuChoiceScanner.nextLine().charAt(0));
System.out.println();

}
}

userMenuChoiceScanner.close();



}

发生的情况是,当用户做出选择时,它会进入适当的方法并在那里完成其任务,然后当它返回到 switch 语句以获取新的用户输入时,它会抛出 java.util.NoSuchElementException错误就像扫描仪流已关闭一样,但我直到最后才关闭扫描仪(流应该仍然打开)。

switch 语句的设置方式是必须先添加一本书(用户必须首先选择“a”),然后才能选择任何其他选项。这是他的代码 addBookToLibrary()方法有几个打开和关闭的扫描仪。我认为关闭这些扫描仪可能会导致问题?

    /**
* Adds new book to library
* @param library ArrayList object which holds book objects
*/
public static void addBookToLibrary(Library library) {
Scanner addBookScanner = new Scanner(System.in);
String title = "";
String author = "";
String genre = "";
String filename = "";
boolean addStatus = false;


/*Asks user for book details */
System.out.println("Enter the details of the book you want to add to the library:");
System.out.println("What is the book title?");
title = addBookScanner.nextLine();
System.out.println("What is the author's name?");
author = addBookScanner.nextLine();
System.out.println("What is book genre?");
genre = addBookScanner.nextLine();
System.out.println("What is the book's filename?");
filename = addBookScanner.nextLine();
addBookScanner.close();

Book newBook = new Book(author, title); //creates new book with author and title set

newBook.setGenre(genre); //sets book genre

File bookFile = new File(System.getProperty("user.dir") + '\\' + filename); //used to check if file user provided exists
System.out.println(bookFile);
/*Checks to see if file user provided actually exists*/
if(bookFile.exists()) {

try {

newBook.setFilename(filename);

}catch(Exception e) {

}

}
else {

Scanner getNewFilenameScanner = new Scanner(System.in);

//Continues to ask user for new filename if file doesn't exist
do {
System.out.println("I'm sorry, but the file you specified does not exist.");
System.out.print("Enter a new file name:");
bookFile = new File(getNewFilenameScanner.nextLine());

}while (!(bookFile.exists()));

getNewFilenameScanner.close();
}

addStatus = library.addBook(newBook); //adds book to library

if(addStatus) {
System.out.println("Book was successfully added!");
}
else {
System.out.println("Book was not successfully added. Please try again.");
}
}

这段代码在以前的项目中运行得很好,所以我不确定为什么它现在不起作用。任何帮助将不胜感激。

非常感谢

最佳答案

在您的 addBookToLibrary 方法中,您可以在使用完 Scanner关闭它们。然而,这些扫描仪连接到标准输入流System.in。根据documentation,关闭这些扫描仪也会关闭标准输入流。 .

还有什么连接到标准输入流? userMenuChoiceScanner!标准输入流关闭后,userMenuChoiceScanner 无法从中读取数据,因此会引发异常。

请注意,尽管 userMenuChoiceScanner 直到非常结束才关闭,但它正在读取的流已关闭。

事实上,您不需要在这里创建多个扫描仪。您只需要使用一台扫描仪,并将其传递给不同的方法。例如,addBookToLibrary 可以接受 Scanner:

public static void addBookToLibrary(Library library, Scanner s) {

它只会使用 s 来读取输入。您可以将您的 userMenuChoiceScanner 传递给它:

addBookToLibrary(library, userMenuChoiceScanner);

作为一般规则,您不应关闭任何非您打开的内容。您没有打开标准输入流(JVM 已经打开了),因此您不应该关闭它。

关于在 switch 语句中读取用户输入时出现 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61041251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com