There really is no one answer of how to validate input that will be applicable to all situations; you have to look at the situation and determine what�s best. The answer will also depend slightly on how far along you are in your learning of
Java (in other words what things have you and have you not learned about) and how robust and future expandable do you want your program to be.
The other question becomes, what do you want to do (or more correctly what does the person asking you to write the program want to do) if the user inputs an invalid value? Exit the application (which some users might find annoying if they simply made a typo) or re-prompt them? If you re-prompt them, do you keep re-prompting until they get it right, or only give them x number of tries, and then exit?
Assuming you want to re-prompt them, but keep it simple, you can do something like this:
The "problem" with that solution is it is bad UI (User interface). Look at this potential session:
Please enter your hours worked
35
Please enter your hours worked
35
Please enter your hours worked
30
Please enter your hours worked
25
Please enter your hours worked
. . .
The user has no idea why they are being re-prompted to enter their hours.
So a better design might be to use the same logic, but simply change the input prompt:
System.out.println ("Please enter your hours worked (40, 45, 50)");
Now the user has an idea of what are valid values. However, that still isn't great. When they input an invalid value, they are still simply re-prompted. They may not understand why. It�s always good design to provide feedback to the user. So we need to initially prompt the user, check the value, then either move on, or print an error and re-prompt:
BTW, as a hint, that last while statement is technically read aloud as " while not is valid". However I always read an "is" named variable with a not operator ('!') such as this as "while is not valid" � just easier to read IMHO. You could also change the variable name and the logic so you use a name like isInvalid and then at the end
test with while(isInvalid)
Now we have a nice routine that will check to see if the value is valid, and properly prompt the user if it is not.
But there are still some things we can do better, more on that in a moment. Notice how in this example, we are simply using an if statement with some Boolean logic to test each of the three possible values. This works ok in a simple example like this since our values are ints and there are only 3 of them. It would also work ok for a range such as if (hours > 0 && hours <=50). But what if we have 10 or 20 possible values? Writing an if statement with 20 or�s in it is just plain messy. So we need to put our possible values into some sort of storage unit and then see if the user�s value is in that storage unit. Later, as you progress through your studies of Java, you�ll learn about collections and the classes of the collection library� and they are storage units for, yup you guessed it, collections of data (or objects). But in this case, since your data are simple ints, we can use an Array:
int[] validHours = {40, 45, 50};
We can then iterate through that array, and see if that the input value is in it. (With many of the collections classes, there is a method named "contains" that can be used to test if a particular value is contained in a collection, so you could do
isValid = validValues.contains(useInput);
But since we are not there yet, we�ll have to manually iterate through the array.
In addition to making it easier to hold multiple values, this last code example has another big advantage. It removes "magic numbers from the code. Magic numbers are numbers (40, 45 and 50 in this case) that are just mysteriously hard coded in a program's logic. They make the program harder to read/understand, more bug prone, and significantly harder to maintain. What if our acceptable values for Hours was coded in with magic number and they appear in 10 different spots in the program? If one day 55 becomes a valid value, you must find all the places they are used (hoping you don�t miss any or change something that looked like a list of valid hours but was not).
By using a final static, you only have to change it in one place. And your code is easier to read. So let me ask you this question as food for thought:
Why do we use a final static array? What are the advantages of that? So there are some ideas for you of how you can validate your input. Now, in addition to the question I just asked, I task you with taking the next step. You have 2 separate pieces of data to prompt your user for. As a result, you will be duplicating 95% of the code used to validate the hours when you go to validate the pay rate. What happens when later you also need to prompt for overtime rate? And then vacation hours? And then sick time? Etc. Do you want to keep putting the same code in over and over again? Probably not; it makes the program harder to read and much harder to maintain.
So give it some thought.
What can you do so that you are not repeating code? [ January 04, 2004: Message edited by: Mark Vender ]