Page 1 of 4 123 ... LastLast
Results 1 to 20 of 63
Like Tree1Likes

Thread: try/catch Help please

  1. #1
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default try/catch Help please

    ....
    Last edited by Goodoleboy58; 05-07-2012 at 02:09 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

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

  4. #4
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default 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 removed
    Last edited by Goodoleboy58; 05-07-2012 at 09:25 AM.

  5. #5
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: try/catch Help please

    Code tags please. :)

  6. #6
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default Re: try/catch Help please

    done!

  7. #7
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: try/catch Help please

    Close but not quite. ([] not <>)

  8. #8
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default Re: try/catch Help please

    one mo' 'gin

  9. #9
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default Re: try/catch Help please

    any input would be welcome

  10. #10
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

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

  11. #11
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

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

  12. #12
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default 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

  13. #13
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: try/catch Help please

    Inside your for loop, where are you checking if the color entered is correct?

  14. #14
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

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

  15. #15
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default 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

  16. #16
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

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

  17. #17
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

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

  18. #18
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default 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"?

  19. #19
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default 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
    Java Code:
    colorcustomerReturn = JOptionPane.showInputDialog( null, "Please select a color Blue, Black, Green, or Gold" );
    This is your validity checking:
    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;
    And this is where you handle the results:
    Java Code:
    switch(returnFlag)
    {
        case 1:
            JOptionPane.showMessageDialog(null, colorvalidEntry);
            break;
     
        case 2:
            JOptionPane.showMessageDialog(null, colorinvalidEntry);
    }
    All of these need to appear only once inside a for loop.

    The following variables are unnecessary:
    Java Code:
    String colorreturnInputMsg, colorInputMsg, 
              customerColor1, color, customerColor2;
    Last edited by brynpttrsn; 05-07-2012 at 05:49 AM.

  20. #20
    Goodoleboy58 is offline Member
    Join Date
    May 2012
    Posts
    34
    Rep Power
    0

    Default Re: try/catch Help please

    results are no better though... If I insert the original correct color it asks me again it runs the first JOption under "ask for input".... Everything else is not effective either... arrgh

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Try... Catch?
    By Callofdudey in forum New To Java
    Replies: 3
    Last Post: 10-06-2011, 09:29 PM
  2. try and catch
    By Bimz in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 09:44 AM
  3. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  4. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 05:39 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •