File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Greetings All! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Greetings All!" Watch "Greetings All!" New topic
Author

Greetings All!

Elaine Banks
Greenhorn

Joined: Dec 16, 2003
Posts: 18
I hope you are all well this evening. I am working ahead for my next course and find that programming is alot more fun without an assignment due on a specific day.
Anyway...
Here's what I'm working on tonight:

=============================================
Please ignore the stuff I have commented out...I may try and use dialog boxes after I get the main logic of the program working....
Anyway...for this assignment there are only supposed to be three valid choices for wages and three valid choices for hours worked. The wages are $7.00, $10.00 and $12.00 per hour. Number of hours are either 40,45 or 50.
What would be the best way to build error checking into this program? All suggestions welcome. Thanks in advance.
Happy New Year to All!
EB
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ January 07, 2004: Message edited by: Dirk Schreckmann ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Hello Elaine,
As a gentle reminder, when you post code on the forum, you should place it between [CODE] and [/CODE] tags. This will give it a nice monospace font for easier reading. It is considerabley easier to read (and debug) code when it is in a monospace font ratehr than a proportional spaced font. Plus as an added bonus, for no extra charge, it will maintain the proper indenting and spacing of your code.
Anyway, when you ask:

What would be the best way to build error checking into this program?

Are you asking:
1) What would be the best way to validate the user input matches the allowable values?
Or
2) What would be the best way to handle the various Exceptions that your code can throw, such as the IOException by br.readLine() and NubmerFormatException by Integer.parseInt(string); ?
Regards,
Mark
[ January 03, 2004: Message edited by: Mark Vender ]
Elaine Banks
Greenhorn

Joined: Dec 16, 2003
Posts: 18
I am asking:
1) What would be the best way to validate the user input matches the allowable values?

Thanks,
CB
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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 ]
Elaine Banks
Greenhorn

Joined: Dec 16, 2003
Posts: 18
WOW!
Thank you so much for a comprehensive and well thought out answer. It was very useful and a joy to read.
EB
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Greetings All!