Results 41 to 60 of 63
Thread: try/catch Help please
- 05-07-2012, 07:40 AM #41
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
- 05-07-2012, 07:41 AM #42
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
- 05-07-2012, 07:55 AM #43
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
trying to incorporate it but it keeps giving me errors.... not sure what i am missing I am trying to add that line and the other lines from your last post... ill post them again here in a sec
- 05-07-2012, 08:02 AM #44
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
you can just kinda get rid of everything under.
throw new Exception();
}
keep the else
- 05-07-2012, 08:04 AM #45
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
"must be caught or declared to be thrown"
Java Code:if(!colorcustomerReturn.matches("[a-zA-Z]*")) { throw new Exception(); } else { JOptionPane.showMessageDialog(null, "Please enter a Valid Color"); }
- 05-07-2012, 08:15 AM #46
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
{} on lines 51 and 69 are not needed.
InputDialogue on line 52 also needs to go.
It should work correctly (asking only once) after that is done.
You will just have input complications if the inputDialogue stays there.
Just report back on if it works exactly how you want it to if entering different types of strings into the input.
- 05-07-2012, 08:26 AM #47
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
The reason:
You dont want to ask for more input before you check what is wrong with the first input.
How the program will run with this line still there:
user enters: thesearenumbers3243158493538
Program outputs: "thats not right"
User enters: Blue
Program outputs: "Good job"
Catch is never reached.
We only want it to ask once at the moment.
- 05-07-2012, 08:28 AM #48
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
Gotcha, input removed, I guess we can incorporate that second inquiry during the loop ehhh... that works good now the way it is laid out
- 05-07-2012, 08:33 AM #49
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Great! We're really close now.
so this means all your booleans and ints need to me moved also.
And I think you missed a few that need to be removed from my quote.
Remove any lines these variables have to do with.
Also, what is valueString for?
(I appologise, but I edit my posts a lot as questions and comments are thought of so you might miss something I've said if you don't reload it after my edits.)Last edited by brynpttrsn; 05-07-2012 at 08:38 AM.
- 05-07-2012, 08:40 AM #50
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
No problem, got booleans and ints lumped together at the top. Deleted valueString, something I was trying to tinker with. Should be good to go on variable placement.
- 05-07-2012, 08:40 AM #51
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
These lines:
need to be moved below your booleans and int declaration.Java Code:colorcustomerReturn = JOptionPane.showInputDialog( null, "Please select a color Blue, Black, Green, or Gold" ); colorinvalidEntry = "We do not have that color. Please select a Valid Color"; colorvalidEntry = "That is a Valid color";
Edit:
Somehow missed your posted code. Just noticed that you did this already... sweet.Last edited by brynpttrsn; 05-07-2012 at 08:48 AM.
- 05-07-2012, 08:46 AM #52
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
got it, sorry had edited the previous post.... caught it right after i reposted it
- 05-07-2012, 08:46 AM #53
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Ok so now you should be able to just pop in a for loop on line 10 (ending on line 69). And that might be it.... maybe.
(You might some more specific error handling in your catch to edit up to do exactly what you want.)Last edited by brynpttrsn; 05-07-2012 at 08:50 AM.
- 05-07-2012, 09:01 AM #54
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Yes... one last thing.
In the for loop your condition is:
i <= 3
We add another condition to make the loop exit when the user enters a correct color like so:
You might get a initialization error while compiling.Java Code:for (int i=1; i <= 3 && returnFlag == 2; i++)
To fix that replace
withJava Code:int returnFlag;
(You have to do that anyway so that the for loop executes)Java Code:int returnFlag = 2;
- 05-07-2012, 09:07 AM #55
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
I've also realized that the comments in your code seem misleading.
When you define a variable, that means this:
Initializing means this:Java Code:int foo; boolean boolStuff;
reading your last post now...Java Code:a = 0; boolStuff = true;
Edit:
Where are your {} for your for loop?Last edited by brynpttrsn; 05-07-2012 at 09:10 AM.
- 05-07-2012, 09:09 AM #56
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
tracking corrected
- 05-07-2012, 09:13 AM #57
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
{} needed for the for loop if you missed my edit.
Edit:
or the { is on the wrong line for the for?
should be on line 12 and 73.
- 05-07-2012, 09:13 AM #58
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
corrected the {} also, sorry you blew my mind like 3 hours ago, im trying to keep up!
Still getting the same first question asked to me 3 times...
edited code should reflelct {}
- 05-07-2012, 09:17 AM #59
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Should look like:Java Code:{ for (int i=1; i <= 3 && returnFlag == 2; i++) //initialize color input
Java Code:for (int i=1; i <= 3 && returnFlag == 2; i++) { //initialize color input
- 05-07-2012, 09:19 AM #60
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
Try... Catch?
By Callofdudey in forum New To JavaReplies: 3Last Post: 10-06-2011, 09:29 PM -
try and catch
By Bimz in forum New To JavaReplies: 1Last Post: 09-26-2011, 09:44 AM -
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch...
By MarkWilson in forum New To JavaReplies: 8Last Post: 06-27-2008, 05:39 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks