提问人:Will Van Zwanenberg 提问时间:12/21/2022 更新时间:12/21/2022 访问量:17
NoSuchElementException - 完全无法修复
NoSuchElementException - completely unable to fix
问:
我正忙于开发一个相对简单的程序。
该程序涉及使用主菜单,该菜单由名为“mainMenu()”的方法创建和显示;其代码包含在我的“Main”类中(本身包含 main() 方法)。此方法通过扫描程序接受输入,并根据用户输入的选项调用相应的方法。
其中一个选项是“quit”(选项 q),如果扫描程序选择 Q 作为用户输入,mainMenu() 方法就会调用一个名为“quitProgram()”的相应方法。
quitProgram() 方法还使用一个 scanner 对象(尽管与 mainMenu() 方法使用的对象不同)。这样做是因为,通过提示,询问用户:“你想退出这个程序吗?是/否?“,该问题的输入被扫描仪捕获。
希望您正在关注所有这些......
关于quitProgram()方法,我遇到了问题。具体来说,如果输入了“n”或“no”,则会引发 NonSuchElementException。如果输入“yes”或“y”,则代码将完美执行。
输入“no”或“n”应该(如果我可以修复这个错误),再次调用mainMenu()方法并将用户带回主菜单,除非它没有。如前所述,它会引发 NoSuchElementException。
我希望通过向您展示这两种方法的代码,您可以告诉我如何解决问题。
mainMenu() 方法:
public static void mainMenu() {
Scanner sc = new Scanner(System.in); // Create a Scanner object.
boolean userInputCorrect = false; // initialise boolean flag.
String choice; // initialise String for holding user input.
// start do ... while loop
do {
// ask user to input their choice of option:
System.out.println("*********************************************************************************************************" + "\r"
+ "PROGRAMMING ASSIGNMENT:" + "\r"
+ "----------------------" + "\r\n" + "\r\n"
+ "STUDENT ID: 2136272" + "\r"
+ "NAME: William van Zwanenberg" + "\r"
+ "MODULE NAME: LAAM39 - Introduction to Programming" + "\r"
+ "MODULE TUTOR: Dr. Livio Robaldo" + "\r\n" + "\r\n"
+ "DESCRIPTION:" + "\r\n"
+ "------------" + "\r\n"
+ "Program to manage a desk and the items on it as per assignment for LAAM39 - Intro to Programming Module." + "\r\n" + "\r\n"
+ "ASSIGNMENT TITLE:" + "\r\n"
+ "-----------------" + "\r\n"
+ "Implement a Java class \"Desk\" with suitable attributes and methods to put and remove items on the desk " + "\r"
+ "and, optionally, other methods to move the desk, buy&sell the desk, etc. Items are instances of another " + "\r"
+ "Java class \"Item\". By using inheritance among classes, you can then optionally distinguish different " + "\r"
+ "kinds of items (notebooks, pens, pencils, etc.)" + "\r\n" + "\r\n"
+ "**********************************************************************************************************" + "\r\n" + "\r\n"
+ "Please enter the corresponding number for the specific action you'd like the program to perfom." + "\r\n" + "\r\n"
+ "YOUR CHOICE OF OPTIONS ARE:" + "\r\n" + "\r\n"
+ "(1) Create a new desk object;" + "\r\n"
+ "(2) Change the properties of an existing desk object;" + "\r\n"
+ "(3) Add a new stationery item to the desk;" + "\r\n"
+ "(4) Remove an existing stationery item from the desk;" + "\r\n"
+ "(5) Change the properties of an existing stationery item;" + "\r\n"
+ "(6) Move the desk" + "\r\n"
+ "(7) Sell the desk and assign it a new owner." + "\r\n" + "\r\n"
+ "To quit the program, type \"q\"." + "\r\n");
choice = sc.nextLine();
// now instigate switch
switch (choice.toLowerCase()) {
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "q":
case "quit":
case "Quit":
case "QUIT":
// change flag becuase user will have made a valid choice
userInputCorrect = true;
// BREAK OUT OF SWITCH
break;
default:
// clear console
clearConsole();
// include countdown delay. Here the for loop merely interates. Nothing more.
for (double i = 1; i<=1000000; i=i+ .01);
// print out error msg:
System.out.println("\r\n" + "ERROR! " + "\""+ choice +"\"" + " is not a valid choice. Please try again." + "\r\n");
} // end of switch
// end the do .. while loop
} while (!userInputCorrect);
// close the Scanner class as we no longer need it.
sc.close();
// flush the content of the buffer to the output stream
System.out.flush();
/*
* If 'q', Q', 'quit', 'Quit', or 'QUIT' is inputed, then quit the program.
* NB: For some reason (UNKNOWN) despite using the 'toLowerCase()'
* String method, the code does not execute
* correctly if "Q" is entered by the user. Therefore, the
* "||" Comparator has been used alongisde the 'equalsIgnoreCase' method in
* to avoid producing an error and forcing the program to quit. This is
* obviously a workaround.
*/
if (choice.equalsIgnoreCase("Q") || choice.equalsIgnoreCase("QUIT")) {
// call quit program method:
quitProgram();
} else {
clearConsole();
System.out.println("\r\n" + "You selected: " + choice);
// now include switch to pass flow to corresponding classes/methods/ depending on the user's selection.
}
} // end of mainMenu() method.
这是 quitProgram() 方法的代码:
public static void quitProgram() {
//clear console:
clearConsole();
// create a new scanner:
Scanner scan = new Scanner(System.in);
// ask user if thwy wish to quit:
System.out.println("Do you want to quit this program? Yes/no?");
// assign user input to a new String:
String options = scan.nextLine();
// initialise boolean for use in while loop:
boolean matchFound = false;
String regex = "yes|no|y|n"; // searches for "yes", "no", "y", and "n" - CASE INSENSITIVE.
while (matchFound == false) {
Pattern pt = Pattern.compile(regex , Pattern.CASE_INSENSITIVE);
Matcher mt = pt.matcher(options);
if (mt.find() == true) {
// match found, therefore set the boolean to true:
matchFound = true;
} else {
// print out error msg:
System.out.println("Invalid entry. Please try again. Do you want to quit this program? Yes/no?");
// get next entry:
options = scan.next();
}
} // end while loop.
// NOW ACT DEPENDING ON SPECIFIC CHOICE:
// close the scanner:
scan.close();
// flush the content of the buffer to the output stream
System.out.flush();
if (options.equalsIgnoreCase("yes") || options.equalsIgnoreCase("y")) {
// clear the Console
clearConsole();
// print out goodbye message:
System.out.println("GOODBYE :-) This program will now quit");
// wait a short while ...
for (double i = 1; i<=10000000; i=i+ .01); // include countdown delay. Here, the for loop merely interates. Nothing more.
// clear console again:
clearConsole();
// now definitely quit.
System.exit(0);
} else if(options.equalsIgnoreCase("no") || options.equalsIgnoreCase("n")) {
// clear console:
clearConsole();
// flush the content of the buffer to the output stream
System.out.flush();
// print out take to main menu message:
System.out.println("OK. Returning you to the main menu ...");
// clear console again:
clearConsole();
// wait a short while ...
for (double i = 1; i<=100000; i=i+ .01); // include countdown delay. Here, the for loop merely interates. Nothing more.
// return to the main menu.
mainMenu();
}
} // end of quitProgram() method.
答: 暂无答案
评论