How can i handle a user entering a String in each of these methods below. I have tried many times using different suggestions and the error keeps looping infinitely. How can i force a user to enter an int instead of a string by adding to the methods below and not modifying them in their present state.
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 errors
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());
}
}
}