提问人:Tonia Giannoulaki 提问时间:4/2/2022 最后编辑:MureinikTonia Giannoulaki 更新时间:4/3/2022 访问量:42
处理 InputMismatchException 异常,但未捕获它 Java
Handle InputMismatchException exception but doesn't catch it Java
问:
由于某种未知原因,当用户输入是字符串或特殊字符时,它不会捕获我的异常(扫描程序只接受整数和 1 到 3)。代码如下:
package mainpackage;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CreateUsers {
public static void main(String[] args) {
Scanner chooseNumber = new Scanner(System.in);
int number;
System.out.println("Welcome to UniStudents Application!\n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor \n"
+ "3. Log in as a Secretary \n"
+ "To choose an action, please write the number of the action: ");
number = chooseNumber.nextInt();
while(true) {
if(number > 3 || number < 1) {
System.out.println("The number of the action does not exist.\n"
+"Please try again: ");
chooseNumber.nextInt();
}
else {
System.out.println("ok");
chooseNumber.close();
break;
}
}
try{
chooseNumber.nextInt();
}
catch(InputMismatchException e) {
System.out.println("Please write the number of the action you want to choose \n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor\n"
+ "3. Log in as a Secretary\n");
}
代码的结果是:
1. Log in as a Student
2. Log in as a Professor
3. Log in as a Secretary
To choose an action, please write the number of the action:
$^&*
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
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 JavaBasics/mainpackage.CreateUsers.main(CreateUsers.java:18)
我想不通
答:
1赞
Mureinik
4/2/2022
#1
第一个调用不在块中,因此不会捕获异常。将块的开头移到它上面,你应该没问题。number = chooseNumber.nextInt();
try
try
1赞
Dylech30th
4/2/2022
#2
由于此异常是在上面的第 18 行抛出的,并且 try-catch 块未涵盖它,因此请尝试:number = chooseNumber.nextInt();
while (true)
package mainpackage;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CreateUsers {
public static void main(String[] args) {
Scanner chooseNumber = new Scanner(System.in);
int number;
System.out.println("Welcome to UniStudents Application!\n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor \n"
+ "3. Log in as a Secretary \n"
+ "To choose an action, please write the number of the action: ");
try {
number = chooseNumber.nextInt();
while(true) {
if(number > 3 || number < 1) {
System.out.println("The number of the action does not exist.\n"
+"Please try again: ");
chooseNumber.nextInt();
}
else {
System.out.println("ok");
chooseNumber.close();
break;
}
}
chooseNumber.nextInt();
} catch (InputMismatchException e) {
System.out.println("Please write the number of the action you want to choose \n"
+ "Please select how to Log in: \n"
+ "1. Log in as a Student \n"
+ "2. Log in as a Professor\n"
+ "3. Log in as a Secretary\n");
}
评论
number = chooseNumber.nextInt();