Why is my first while loop checking the validity of input from the user but the other two while loops for month and year are just being skipped over (ignored)? I'm trying to get a user to enter a date correctly and not have them enter impossible values.
// A read() method to read in the account details from the user
boolean success = false;
public void read()
{
Scanner keyboardIn = new Scanner(System.in);
System.out.println("ENTER ACCOUNT DETAILS: ");
System.out.print("User Title: ");
String title = keyboardIn.nextLine();
name.setTitle(title);
System.out.print("User First name: ");
String firstname = keyboardIn.nextLine();
name.setFirstName(firstname);
System.out.print("User Second name: ");
String secondname = keyboardIn.nextLine();
name.setSurname(secondname);
System.out.print("Account Address: ");
address = keyboardIn.nextLine();
// To make sure day is entered correctly (1 - 31)
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());
}
}
// To make sure month is entered correctly (1 - 12)
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());
}
}
// To make sure year is entered correctly (< 1900 not permitted)
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());
}
}
System.out.print("Enter the initial balance: ");
balance = keyboardIn.nextDouble();
System.out.print("Enter the overdraft amount: ");
overdraftAmount = keyboardIn.nextDouble();
System.out.println("Account number: " + accountNo);
System.out.println();
}