![]() |
Little Error checking help
I've got two methods which take in information from the user. One which takes in Strings:
:
public static String getNames(){and one which takes in Integers: :
public int getNums()The problem is that if the user enters a string when the system expects an integer, there is an error and the system crashes. Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "r" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) Any idea on what to write to ensure the user enters an integer when asked? |
"Please enter a number from [min] to [max]....if you don't enter a number this program will crash":D
|
While that seems like a joke, there is a large measure of truth involved. YOU DO NOT HAVE CONTROL OF YOUR USER. Instead, you use a function that tells you whether or not the user complied with your requirements. Then you check what the function had to say. If your function doesn't have that ability, the function was written by a schlock. If your function has that ability, but you don't use it, you are either uninformed, or a schlock.
|
Just have another catch statement to catch the NumberFormatException!
:
public int getNums() |
| All times are GMT -5. The time now is 2:04 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC