尝试捕获不起作用的Java



我正在尝试在 JAVA 中使用 try-catch 语句在 while 循环中,只是为了在将字符串输入提供给 nextInt 时捕获异常,我不知道为什么它在第一个错误输入后不断引发异常。

法典

import java.util.Arrays;
import java.util.Scanner;
public class People{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Scanner inS=new Scanner(System.in);
int i=0;
System.out.println("Enter number of people");
int n=in.nextInt();
int[] age=new int[n];
String[] name=new String[n];
double[] aIncome=new double[n];
while (i<n){
try{
System.out.println(i+"Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : ");
name[i]=inS.nextLine();
System.out.println("Now putting name");
System.out.println("Enter your age: ");
age[i]=in.nextInt();
System.out.println("Now putting age");
System.out.println("Enter your annual income: ");
aIncome[i]=in.nextDouble();
System.out.println("Now putting income");
i++;
}
catch(Exception InputMismatchException){
System.out.println("Error !! Wrong input, Please try again.");
}
}
System.out.println(Arrays.toString(age));
System.out.println(Arrays.toString(name));
System.out.println(Arrays.toString(aIncome));
}
}

当给出正确的输入时。

Script started on Sat 03 Feb 2018 07:55:42 PM NST
amehla@slbnen3000pc50:~/Desktop/test$ jva KKKava People.K
Enter number of people
3
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
harit Gupta
Now putting name
Enter your age: 
26
Now putting age
Enter your annual income: 
59495
Now putting income
1Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
Rahul Gupta
Now putting name
Enter your age: 
59
Now putting age
Enter your annual income: 
35695
Now putting income
2Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
Shikha kom
Now putting name
Enter your age: 
56
Now putting age
Enter your annual income: 
59565
Now putting income
[26, 59, 56]
[harit Gupta, Rahul Gupta, Shikha kom]
[59495.0, 35695.0, 59565.0]

但是当给出不正确的输入时,这种情况会无限次。

amehla@slbnen3000pc50:~/Desktop/test$ java People
Enter number of people
3
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
Rahul Gupta
Now putting name
Enter your age: 
59
Now putting age
Enter your annual income: 
fasdf
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
Rahul Gupta
Now putting name
Enter your age: 
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
Rahu lasdf
Now putting name
Enter your age: 
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
asdf adsf
Now putting name
Enter your age: 
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") : 
^Camehla@slbnen3000pc50:~/Desktop/test$ exit
exit
Script done on Sat 03 Feb 2018 07:57:22 PM NST

即使有人回复,也会有很大的帮助。

谢谢。

当你混合和匹配nextLine()nextInt()nextDouble()或任何这些时,换行符会有一些奇怪的事情发生。如果将代码中对nextInt()nextDouble()的调用替换为Integer.valueOf(inS.nextLine())Double.valueOf(inS.nextLine()),这应该可以解决您的问题。

作为一般规则,如果要在命令行读取任何用户输入,则应读取整行并分析整行。不要试图抓住单个代币,它会变得非常混乱。

我尝试运行您的代码,我发现了三个可以解决您的问题的更改。

首先 - 您不需要两个扫描仪。只需使用一个。完全摆脱in2

接下来 - 摆脱in2有点搞砸生产线name[i]=inS.nextLine();您可以通过将其更改为以下内容来修复它:name[i]=in.next();我不确定为什么这会解决问题,但这是我最好的猜测(其他用户,如果您对此了解更多,请说出来!

当你使用nextLine()时,扫描仪会查找下一个n字符之前的所有内容。在前面对int n=in.nextInt();行中的扫描仪的调用中,扫描仪仅读取数字本身,而不读取用户在输入数字时输入的相应n换行符。因此,调用in.nextLine()将导致扫描程序读取应再次作为名称的相同值。我们不希望这样 - 我们只想读取用户输入的下一个令牌next()做得非常好。

最后 - 我还更改了行catch(Exception InputMismatchException){...为更传统的编写 catch 语句的方式:catch(InputMismatchException e){...

当您键入输入值时,您可以通过按Enter键接受它。它在输入的值后生成换行符。在调用该方法的下一行代码中nextLine()时,它会读取剩余的新行字符,而不是键入的输入。要在调用nextInt()nextDouble()后清除换行符,请将nextLine()放在这些方法之后。

另一种可能性是调用Integer.parseInt(in.nextLine())Double.parseDouble(in.nextLine())等方法,因为它们会自动删除剩余的新行字符。

您还应该考虑将in.nextLine()方法放入catch块中,以便在提供抛出异常的输入时删除剩余的新行字符:

代码中存在的另一个问题是这部分:

catch(Exception InputMismatchException) {...}

使用该代码,您可以捕获任何类型的Exception- 不仅是InputMismatchException,而且从类和Exception本身继承Exception的任何其他类型 - 首先在括号中是您想要捕获的Exception类,然后是您要在块中调用它的名称catch- 将其更改为:

catch(InputMismatchException ex ) {...}

这样你只会抓住InputMismatchException.

我不太明白问题,但我想你想要的是抛出第一个异常后退出循环?然后而不是有

while (...) {
try {
...
} catch (...) {
...
}
}

你可以写

try {
while (...) {
...
}
} catch (...) {
...
}

或者只需在 catch 块中添加break语句即可退出周围的while循环。

最新更新