提问人:LisaMoon 提问时间:8/14/2023 最后编辑:LisaMoon 更新时间:8/14/2023 访问量:43
线程“main”java.util.NoSuchElementException中的异常:未找到行 Project Java Eclipse IDE
Exception in thread "main" java.util.NoSuchElementException: No line found Project Java Eclipse IDE
问:
我正在使用 Eclipse IDE 开发 Java 程序,并遇到“线程 'main' java.util.NoSuchElementException: No line found”错误的问题。此错误发生在我的控制台输出中
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Controller.Client_Controller.menu(Client_Controller.java:94)
at Controller.Main_Controller.menu(Main_Controller.java:38)
at Controller.EShop2023.main(EShop2023.java:17)
该错误专门链接到以下代码行:sc.nextLine();
行后:System.out.println(“无效输入。请输入一个有效的号码。
以下是菜单处理代码的相关部分:
package Controller;
import java.util.ArrayList;
import java.util.Scanner;
import Model.Client;
public class Client_Controller {
private ArrayList<Client> clientList = new ArrayList<>();
private int i_Current; // indice element en cours
private int i_First; // indice premier element affichable
private int i_Last; // indice dernier element affichable
private int i_Aff; // valeur elements affichables
public static void main(String[] args) {
Client_Controller controller = new Client_Controller();
controller.menu();
}
public void menu() {
int i_Choice = 0;
Scanner sc = new Scanner(System.in);
do {
if (i_Aff != 0) {
do {
C_CheckList();
System.out.println("\nClient's Menu");
System.out.println("***************");
System.out.println("1. Create");
System.out.println("2. Update");
System.out.println("3. Delete");
System.out.println("4. Next");
System.out.println("5. Previous");
System.out.println("6. Last");
System.out.println("7. First");
System.out.println("8. Select Client");
System.out.println("9. Exit");
System.out.print("Your choice ? : ");
if (sc.hasNextInt()) {
i_Choice = sc.nextInt();
sc.nextLine();
switch (i_Choice) {
case 1:
if (Client.getI_Pk() > 99) {
System.out.println("Limit reached");
} else {
C_Create(sc);
}
break;
case 2:
C_Update(sc);
break;
case 3:
clientList.get(i_Current).setB_Is_Deleted(true);
i_Aff--;
C_CheckList();
i_Current = i_Last;
break;
case 4:
C_Next_Client();
break;
case 5:
C_Previous_Client();
break;
case 6:
i_Current = i_Last;
break;
case 7:
i_Current = i_First;
break;
case 8:
C_Select();
break;
case 9:
System.out.println("Back to main menu...");
break;
default:
System.out.println("Wrong Choice, please select a new number");
}
} else {
System.out.println("Invalid input. Please enter a valid number.");
sc.nextLine();
}
} while (i_Choice < 0 || i_Choice > 9);
} else {
do {
System.out.println("\nEmpty list");
System.out.println("Customer's Menu");
System.out.println("***************");
System.out.println("1. Create");
System.out.println("9. Exit");
System.out.print("Your choice ? : ");
if (sc.hasNextInt()) {
i_Choice = sc.nextInt();
sc.nextLine();
switch (i_Choice) {
case 1:
C_Create(sc);
break;
case 9:
System.out.println("Back to main menu...");
break;
default:
System.out.println("Wrong Choice, please select a new number");
}
} else {
System.out.println("Invalid input. Please enter a valid number.");
sc.nextLine();
}
} while (i_Aff == 0);
}
} while (i_Choice != 9);
}
我把System.out.println(“无效的输入。请输入一个有效的号码。因为否则控制台中的菜单会变得“疯狂”并不断上下移动而无法执行任何操作。 把这句话放在控制台发疯的时候:
if (sc.hasNextInt()) {
i_Choice = sc.nextInt();
sc.nextLine();
提前感谢您提供任何可能的建议来帮助我解决这个问题。
答: 暂无答案
评论