import java.util.Scanner;
import java.util.InputMismatchException;
public class backup {
public static int t1;
public static int t2;
public static int x;
public static int y1;
public static int m1;
public static int d1;
public static int y2;
public static int m2;
public static int d2;
public static void date1() {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Please enter the first date ");
System.out.println("Please enter the year: ");
y1 = scanner.nextInt();
System.out.println("Please enter the month: ");
m1 = scanner.nextInt();
System.out.println("Please enter the day: ");
d1 = scanner.nextInt();
} catch (InputMismatchException inputMismatchException)
{
scanner.nextLine();
System.err.println("You must enter intergers. Please try again. "); }
}
public static void caldate1() {
int j = 693502;
if (t1 > j) {
if (m1 == 1 + 3 + 5 + 7 + 8 + 10 + 12) {
t1 = ((365 * y1) + d1 + 31);
} else if (m1 == 2) {
t1 = ((365 * y1) + d1 + 28);
} else if (m1 == 4 + 6 + 9 + 11)
;
{
t1 = ((365 * y1) + d1 + 30);
}
}
else {
System.err.printf("Error. Please enter a date after Jan 1st 1900.\n");
}
}
public static void main(String[] args) {
date1();
caldate1();
}
}
The second error appears when I cause an error in the first method. Does anyone know what can cause this? Or what I am doing wrong? It should catch errors if user enters anything other than a integer. Also does anyone know how I can set a loop to each of the methods when the error comes into effect?
Firstly:
m1 == 4 + 6 + 9 + 11is equal tom1 == 30. I think you meantm1 == 4 || m1 == 6 || m1 == 9 || m1 == 11Secondly:
This line
if (t1 > j)never be true, because you never sett1, so it’S zero and zero can’t be bigger than 693502.