Java 在While循环中处理输入不匹配异常
因此,我有一个while循环,其中有3个选项可供选择,您可以通过使用扫描仪在标准输入上插入一个数字来选择它们,我的代码如下:Java 在While循环中处理输入不匹配异常,java,exception,while-loop,java.util.scanner,Java,Exception,While Loop,Java.util.scanner,因此,我有一个while循环,其中有3个选项可供选择,您可以通过使用扫描仪在标准输入上插入一个数字来选择它们,我的代码如下: int option; String request; Scanner input2 = new Scanner(System.in); System.out.println("Choose an option:\n" + "1-Get camera information\n" + "2-Submit Data\n" +
int option;
String request;
Scanner input2 = new Scanner(System.in);
System.out.println("Choose an option:\n" + "1-Get camera information\n" + "2-Submit Data\n"
+ "3-Exit");
while(true){
try {
option = input2.nextInt();
if (option == 1) {
System.out.println("Camera name:");
request = input2.nextLine();
while (request.length() < 3 || request.length() > 15) {
System.out.println("Name has to be between 3 and 15 characters, insert a new one:");
request = input2.nextLine();
}
CamInfoRequest info_request = CamInfoRequest.newBuilder().setName(request).build();
if (stub.camInfo(info_request).getReturn() != 0) {
System.out.println("Camera does not exist");
} else {
System.out.println(stub.camInfo(info_request).getLatitude() + " " + stub.camInfo(info_request).getLongitude());
}
} else if (option == 2) {
System.out.println("submit");
} else if(option ==3){
break;
} else{
System.out.println("Invalid option.");
}
}catch(InputMismatchException e){
System.out.println("Invalid input");
}
}
int选项;
字符串请求;
扫描仪输入2=新扫描仪(System.in);
System.out.println(“选择一个选项:\n“+”1-获取相机信息\n“+”2-提交数据\n”
+“3-退出”);
while(true){
试一试{
option=input2.nextInt();
如果(选项==1){
System.out.println(“摄像机名称:”);
request=input2.nextLine();
while(request.length()<3 | | request.length()>15){
System.out.println(“名称必须介于3到15个字符之间,插入一个新的:”);
request=input2.nextLine();
}
caminformequest info_request=caminformequest.newBuilder().setName(request.build();
if(stub.camInfo(信息请求).getReturn()!=0){
System.out.println(“摄像机不存在”);
}否则{
System.out.println(stub.camInfo(信息请求).getLatitude()+“”+stub.camInfo(信息请求).getLatitude());
}
}否则如果(选项==2){
系统输出打印项次(“提交”);
}否则如果(选项==3){
打破
}否则{
System.out.println(“无效选项”);
}
}捕获(输入不匹配异常e){
System.out.println(“无效输入”);
}
}
因此,当代码捕捉到异常并不断打印“无效输入”时,代码进入无限循环,我尝试在捕捉时使用input2.next(),但他等待另一个我不想要的输入,我也不能使用input2.close()。我能做什么?只需将Scanner语句放在try块中即可
while (true) {
try {
Scanner input2 = new Scanner(System.in);
option = input2.nextInt();
if (option == 1) {
我也不能使用input2.close()
切勿关闭系统中的扫描仪
实例,因为它也会关闭系统中的实例
我尝试在catch中使用input2.next(),但他却在等待另一个
输入我不想要的
使用Scanner::nextLine
代替Scanner::next
,Scanner::nextInt
等。检查了解原因
此外,如果输入无效,请在需要要求用户再次输入数据的任何位置尝试使用do…while
下面给出了包含以下几点的示例代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int option;
boolean valid;
Scanner input2 = new Scanner(System.in);
do {
valid = true;
System.out.println("Choose an option:\n" + "1-Get camera information\n" + "2-Submit Data\n" + "3-Exit");
try {
option = Integer.parseInt(input2.nextLine());
if (option < 1 || option > 3) {
throw new IllegalArgumentException();
}
// ...Place here the rest of code (which is based on the value of option)
} catch (IllegalArgumentException e) {
System.out.println("This is an invalid entry. Please try again.");
valid = false;
}
} while (!valid);
}
}
如果有任何进一步的疑问/问题,请随时发表评论。您的第一个错误是使用while(true)
。改为使用布尔标志。将看起来像while(exitLoop)
然后将exitLoop=true
设置为要离开循环的位置。无限打印会以这种方式停止,问题是假设我启动循环,选择无效输入,然后选择选项1,它会立即进入while并打印名称必须介于3到15个字符之间,请插入一个新字符:“在我甚至可以写东西之前,请尝试使用request=input2.next();而不是request=input2.nextLine();
Choose an option:
1-Get camera information
2-Submit Data
3-Exit
abc
This is an invalid entry. Please try again.
Choose an option:
1-Get camera information
2-Submit Data
3-Exit
6
This is an invalid entry. Please try again.
Choose an option:
1-Get camera information
2-Submit Data
3-Exit
2