Help making an error message.
I am new to programming and need help creating an error message for one of my programs. My question is how do I make java show an error if the user were to enter letters instead of a number and how can I make a loop to keep asking for the input and show the error as long as the input is not a number.
The following is only part of my code and the only part I want to show an error message for incorrect input.
Code Java:
String YearString = JOptionPane.showInputDialog(null,
"Please enter a year, for example 1700:",
"Finding Easter",
JOptionPane.QUESTION_MESSAGE);
int YEAR = Integer.parseInt(YearString);
Thanks in advance to all who helped!
Re: Help making an error message.
Quote:
My question is how do I make java show an error if the user were to enter letters instead of a number and how can I make a loop to keep asking for the input and show the error as long as the input is not a number.
A while loop would work in this scenario, looping until the year string validates. Rather than catching the exception, you could simply validate the string. For example in pseudo-code:
Code :
String value = "";
while ( isValid(value) ){
value = //optionPane input
}
private boolean isValid(String s){
//validate the string for digits.
}
Re: Help making an error message.
Thank-you for the assistance!