My read() methods crash each time I enter a String or Char. How would I get it to accept only integer values. When I enter an int, it works fine but when I enter a Char or String I get a repetitive "Enter the day the account opened: null" error. I have to terminate the program to stop it.
private void readDay(Scanner keyboardIn) {
boolean success = false;
while (!success) {
try {
System.out.print("Enter the day the account opened: ");
int d = keyboardIn.nextInt();
dateOpened.setDay(d);
success = true;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
// Enter the month, checking for error
private void readMonth(Scanner keyboardIn) {
boolean success = false;
while (!success) {
{
try {
System.out.print("Enter the month the account opened: ");
int m = keyboardIn.nextInt();
dateOpened.setMonth(m);
success = true;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
// Enter the year, checking for error
private void readYear(Scanner keyboardIn) {
boolean success = false;
while (!success) {
try {
System.out.print("Enter the year the account opened: ");
int y = keyboardIn.nextInt();
dateOpened.setYear(y);
success = true;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}