Demonstration of building robust code for user input validation of numeric types since Java GUI components supply String objects as their return type. This project uses JOptionPane.showInputDialog to demonstrate how to retrieve numeric input from the user and supply 'bad' default values if an incorrect type is supplid. Using a try/catch block and if/else blocks are specically addressed.
Table of Contents:
00:20 - Code before defensive methods
00:41 - All GUI fields provide String values
00:55 - Helper methods are private
01:14 - default variable with value
01:44 - return the declared variable!
02:28 - Using the try statement
03:12 - The code to attempt
04:04 - Catch the exception
05:11 - Test the try/catch
05:29 - Test if the value is parseable
05:43 - Assign correct value if parseable
06:15 - Assign default value if not parseable
06:49 - Test the defensive code
07:03 - Input value
07:37 - Put input into the answer variable
07:46 - Display the returned String
08:12 - Prompt for numeric input
08:23 - Show what happens with invalid input
08:30 - Notify user of invalid input and assign default
09:01 - Enter the catch block
09:05 - Return false since the test failed
09:15 - Assign the incorrect value
10:14 - Validate double input
11:09 - define variable, then return variable
12:43 - Validation methods are the same
13:19 - Use the validation method
15:59 - Defensive programming review
16:28 - This verification method will be needed in other projects
16:58 - Project Review