Results 1 to 20 of 63
Thread: try/catch Help please
- 05-06-2012, 04:50 PM #1
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
- 05-06-2012, 04:55 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: try/catch Help please
If your input field isn't empty your code considers it as valid input; I doubt that you want that.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-06-2012, 05:06 PM #3
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
Gotcha, I understand that part... again my Java understanding is very basic. My issue is that if I make that statement false the program doesnt run at all and errors out at this point. Which I reckon means the rest of my code here is all jacked up.
- 05-07-2012, 02:15 AM #4
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
ok I've been working on this all day and here's what I got.... It runs, and I only accept the proper colors, however there is an issue with my if/then and catch statements. If I put the wrong color in first, i.e. Brown, it tells me that is not a valid color. If I put a correct color in i.e. Blue, it still tells me that it is not a correct color and goes to the catch statement and ends the program. If I put Blue in to begin with it works fine and tells me that is a valid color. What am I missing?
.... code removedLast edited by Goodoleboy58; 05-07-2012 at 09:25 AM.
- 05-07-2012, 02:30 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Code tags please. :)
- 05-07-2012, 02:32 AM #6
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
done!
- 05-07-2012, 02:38 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Close but not quite. ([] not <>)
- 05-07-2012, 02:41 AM #8
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
one mo' 'gin
- 05-07-2012, 03:24 AM #9
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
any input would be welcome
- 05-07-2012, 03:29 AM #10
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Inside your for loop you don't check for a valid color correctly. The way you have it right now checks it against the original input.
- 05-07-2012, 03:41 AM #11
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
changed the value of the loop to only check for empty string... however now when i run it, it asks me for a color, and if i put the right color in the first time its good. If I put the wrong color in, and then any color the second time it just stops the program. Doesn't error out, just stops running.
code removed .....Last edited by Goodoleboy58; 05-07-2012 at 09:25 AM.
- 05-07-2012, 03:42 AM #12
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
im sure there has to be an easier way to do all of this, I appreciate you sticking around and looking it over
- 05-07-2012, 04:04 AM #13
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
Inside your for loop, where are you checking if the color entered is correct?
- 05-07-2012, 04:22 AM #14
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
I am not sure what I am doing with that as far as checking for correctness... I currently have it checking to weed out empty fields, would I add something like this:
code removed ......Last edited by Goodoleboy58; 05-07-2012 at 09:26 AM.
- 05-07-2012, 04:23 AM #15
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
i know the above isnt right as far as what to do with the color if it is correct.... I guess thats where my brain is stuck
- 05-07-2012, 04:28 AM #16
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
even without the for loop, which I should only hit if i leave the field empty, the other field is jacked up in the sense that after the initial entry it takes any entry.
Seemingly the only thing I have working is the original return flags and case 1....
- 05-07-2012, 04:31 AM #17
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
since the first check works correctly, you can jost encase your validity checking for the first check in your loop.
- 05-07-2012, 04:33 AM #18
Member
- Join Date
- May 2012
- Posts
- 34
- Rep Power
- 0
Re: try/catch Help please
hate to sound ignorant but the reason its long and drawn out the way I have it, is because its the only way I understand how to do it at this time. I am working on improving my knowledge, but in the meantime can you offer some suggestions on how to "encase your validity checking for the first check in your loop"?
- 05-07-2012, 04:52 AM #19
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: try/catch Help please
No problem. :)
Sometimes if you have code that's not working you should take a step back and think only about what you want the code to do and see if you can work it out from there.
Ok so what you want to do is check the users input 3 times. That's just a simple for loop that counts to 3.
Inside the for loop you want to:
1. Ask for input
2. Check if the input is valid
3. Handle true or false accordingly
If it is valid you want to exit the loop
If not, just let the loop continue.
You have the right idea with your for, but its not in the right place.
I would only have the input in one place. Right now you have Input in 3 separate places.
You're really close where it's at right now. I think you actually have too much code.
You have all the elements there that you need, they just need to be rearranged.
Just change your for so that your checking and asking for input is inside it.
(You dont need anything thats in your for right now. Get rid of it.)
To clarify:
This is your input
This is your validity checking:Java Code:colorcustomerReturn = JOptionPane.showInputDialog( null, "Please select a color Blue, Black, Green, or Gold" );
And this is where you handle the results:Java Code:colorreturnBlue = colorcustomerReturn.equalsIgnoreCase("Blue"); colorreturnGreen = colorcustomerReturn.equalsIgnoreCase ("Green"); colorreturnGold = colorcustomerReturn.equalsIgnoreCase ("Gold"); colorreturnBlack = colorcustomerReturn.equalsIgnoreCase ("Black"); if (colorreturnBlue) returnFlag = 1; else if (colorreturnGreen) returnFlag = 1; else if (colorreturnGold) returnFlag = 1; else if (colorreturnBlack) returnFlag = 1; else returnFlag = 2;
All of these need to appear only once inside a for loop.Java Code:switch(returnFlag) { case 1: JOptionPane.showMessageDialog(null, colorvalidEntry); break; case 2: JOptionPane.showMessageDialog(null, colorinvalidEntry); }
The following variables are unnecessary:
Java Code:String colorreturnInputMsg, colorInputMsg, customerColor1, color, customerColor2;Last edited by brynpttrsn; 05-07-2012 at 05:49 AM.
- 05-07-2012, 05:49 AM #20
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