There are a bunch of threads throughout the internet on this but I can't make heads or tails of any of them.
As an assignment for an intro to Java course we have been tasked with making a handful of different programs, we were then given them back and told to make them idiot proof.
So for example, we have to use a program to determine what day of the week a random day of any random year will be, (i.e Jan 1 2013 being a Tuesday)
I prompt for three things, Day, Year and Month, I want to make it so that if Day is contains a letter it sends back Invalid and prompts again.
Right now I have it set so that if day is an integer less than one or greater than 31 it asks again, so I don't have a problem with the range, just the NFE.
I have heard that I should use a
Try
{
//...
}
catch{NumberFormatExcept nFE}
but I have no idea how to use that to re-prompt for what I an looking for
Here is a snippet of my code so far
System.out.print("Enter the day of the month (1-31): ");
int d = UserInput.nextInt();
do
{
if(d < 1 || d > 31)
{
System.out.print("That day is invalid, please enter a day between 1 and 31");
d = UserInput.nextInt();
}
}while(d < 1 || d > 31);
I tried making d a string and using Integer.parseInt(); but that would just parse a into 1, I want to so something like if d.hasNextInt(); continue, but if it hasNextString() reprompt.
AKA
String d;
d = UserInput.nextLine();
int dVar = Integer.parseInt(d);
I can't just throw an exception because the objective is to not crash but just prompt again.