public void processing()
{
System.out.println("Please enter the amount of saving per month: ");
try
{
while(input.hasNext())
{
setSavingPermonth(input.nextDouble());
System.out.println("Please enter expected interest rate: ");
setInterestRate(input.nextDouble());
System.out.println("Please enter number of month for saving: ");
setNumberOfMonth(input.nextInt());
displayInputs();
System.out.println();
totalContibution();
totalSavingAmount();
System.out.println();
System.out.println("\nPlease enter the amount of saving per month: \n(or <Ctrl + D> to close the program)");
}
}
catch(InputMismatchException inputMismatchException)
{
System.err.println("Input must be numeric. \ntry again: ");
//**Anything need to done here in order to go back to setInterestRate()?**
}
}
I want to go back to where before the exception caught the mis-input exception, for example, I type in a string for the setInterestRate(), but Java catches me there, and display the error message, question: how can I go back to there so that I can re-enter a correct data ?
Rewrite your code so it uses the following method: