Goto and user errors
Okay, start off by saying, i'm new to java. A lot of it confuses. Please bare with me as I make mistakes and won't understand things at first lol. For the following solutions please try to explain why what you did worked though as I am trying to learn with this, not actually make a useful program.
Heres the the current program, a basic calculator built to work through JOptionPane panels, I'll explain my problem at the bottom:
The second question is... and I know i'm going to be hated for this... How do I get around java not having goto? I'm rather used to it and I don't know how to use java to return to the top of the program, perhaps some kind of loop method? I want to make it so if the user presses Yes on "JOptionPane.showConfirmDialog(null, "Do More Calculations?", "Calculator", JOptionPane.YES_NO_OPTION);" it returns to the top.
Thanks ahead of time!
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.
Sorry it took me so long to get back, and thanks for helping, however I don't fully understand.
I would assume goes by the class start, as my prior experience with exceptions such as "FileNotFoundException" and would start with the throws identifier. This would however send back the error and not resolve the problem.
"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."
I've never seen this method before, would it be like an if and be like...
I understand the Do/While part, however I don't understand how to change a variables value by pressing the YES/NO buttons so if you could help with that too thanks ha.
Thank you so much for all the help btw.
That would be like:
JOptionPane.showConfirmDialog will return a result depending on what is clicked. Give it yes/no only options, and compare the results in the while clause:
|All times are GMT +1. The time now is 03:58 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.