I am trying to use try-catch statement in JAVA in a while loop, just to catch the exception when string input is given to nextInt, I don't know why it keeps on raising the exception after the first incorrect input.
Code
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));
}
}
When correct input is given.
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]
But when incorrect input is given, this goes on infinite times.
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
Would be a great help, Even if someone replies.
Thank You.