Scanner scan = new Scanner(System.in);
int year = -1;
int yearDuplicate = -1;
System.out.println("This calculator tells you whether a year that you "
+ "enter is a leap year.\nPlease enter a year:");
do{
try{
year = scan.nextInt();
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(year % 1 != 0);
do{
if(yearDuplicate % 4 == 0){
if(yearDuplicate % 100 == 0){
if(yearDuplicate % 400 == 0){
System.out.println(yearDuplicate + " is a leap year.");
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
}else{
System.out.println(yearDuplicate + " is a leap year.");
}
}else{
System.out.println(yearDuplicate + " is not a leap year.");
}
System.out.println("Enter another year or enter '-1' to quit:");
try{
yearDuplicate = scan.nextInt();
}catch(Exception e){
System.out.println("ERROR. Please enter a numerical digit "
+ "without decimals:");
scan.next();
}
}while(yearDuplicate != -1);
I am trying to make a leap year calculator that also tells you whether you accidentally typed in a string or double instead of an integer. What I would want this program to do is run the "ERROR. Please enter a numerical digit without decimals:" over and over again until the user enters in an integer. However, currently, for the first try catch block, if I input two wrong input types, it gives me this error message:
test
ERROR. Please enter a numerical digit without decimals:
-1 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
BUILD SUCCESSFUL
The second one is at least slightly better, it keeps repeating the result of the last integer I put in (year "is a/not a leap year") but at least it repeats forever unlike the first one.
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
All I want in for error message to get replayed until you input in the correct number. How do I get rid of the previous input result and how do I fix the fact that the first try catch block only runs twice?
I looked online but it seems if I change one small part of this program, something else breaks. It does exactly what I want it to if I make the condition of the first while loop to be while(year % 1 == 0); but then that means that if it is an integer, the while loop repeats which is the exact opposite of what I want it to do. If I mess with the scan.next();, it'll give me an infinite error loop.
Btw, there are two try catch blocks since there are two opportunities for the user to enter in a year and two places where I might have to correct them. If I put them both in the same while loop, it'll work the first time but then after that, every time I need to enter in two numbers (one for the first try catch which gets overwritten by the second try catch).
Edit: I changed the starting values of year and yearDuplicate to 0, declared the scanner inside the try catch block and got rid of the scan.next(); in both try catch blocks. Now, I'm getting a loop with the first try catch block but it's still carrying over my previous input.
year % 1 != 0
to be? – user14387228