View Single Post
Old 10-09-2012, 02:34 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
To prevent errors you must use a try/catch. You need NumberFormatException to prevent a failed cast to double. You can use a generic Exception catch and use instanceof checks to see exactly what it is as well, so you can throw custom exceptions for any and all of the bad checks if desired.
Arrays can be counted with a .length property. Use this to your advantage.
Finally use a do/while to return control back to the top. Use a sentinal do/while and accept the input from the confirm dialog. Use an integer and compare to the YES_OPTION of the JOptionPane to determine if it should continue. This integer needs to be declared before the do/while in order to use it in the while.

Aside from this, regex could be used to determine if the string matches a valid pattern before parsing. Input to performance though, I'd expect that a try/catch would beat a pattern match for this.
Fou-Lu is offline   Reply With Quote