|
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.
|