Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 63
Like Tree1Likes

Thread: try/catch Help please

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

    Default Re: try/catch Help please

    " //intialize return flag"
    This is your validity checking

    You don't need a default case as it can only be 1 or 2 at this point in the code.
    The only line needed in your case 2: is
    Java Code:
    JOptionPane.showMessageDialog(null, colorinvalidEntry);
    Get rid of all of your inputDialogues accept for the first one.(line 6)
    Forget about the loop right now. Just work on getting the first check working.
    (Which it does)


    " //validity check"
    Delete everything after this(before the catch)

    Trust me I'm a doctor.
    Last edited by brynpttrsn; 05-07-2012 at 06:05 AM.

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

    Default Re: try/catch Help please

    Are there compile errors? that should be working fine.

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

    Default Re: try/catch Help please

    no, no errors at all.... I can put anything, numbers, symbols, into the "colorcustomerReturn" field and it proceeds to case 2, nothing I put in can make it go to activate the catch...

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

    Default Re: try/catch Help please

    Well yes.
    returnFlag = 2;
    This makes it go to case 2 if anything but the correct colors are input.
    Is this what it is supposed to do or is it supposed to do something different?
    In what circumstances is it supposed to reach the catch?
    Last edited by brynpttrsn; 05-07-2012 at 06:25 AM.

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

    Default Re: try/catch Help please

    Ultimate goal, I would like to reprompt to put in the correct color.

    Prompt the user to put in the correct color.

    If the user inputs something other than alpha then I would like it to go to the catch statement and tell them to input a color.

    If the user inputs something invalid 3 times I would like to system.exit.

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

    Default Re: try/catch Help please

    Again, I want to thank you for sticking with me. I tried countless times to pay for a tutor online and all of them were worthless, wanted to do it for me for $40, but that doesn't teach me to fish...

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

    Default Re: try/catch Help please

    "If the user inputs something other than alpha then I would like it to go to the catch statement and tell them to input a color."

    If the user doesnt input a correct color you just want to ask them the same question that you asked the first time right?
    Or Is it a different question?

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

    Default Re: try/catch Help please

    No same question is fine

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

    Default Re: try/catch Help please

    i know i can reask the same question by adding

    JOptionPane.showInputDialog( null, "Please select a color Blue, Black, Green, or Gold" );

    but the adding the loop and getting the try/catch to work is the tricky part for me

    basically the validation of the entry past the original question

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

    Default Re: try/catch Help please

    If youre going to ask the same question then all you need is a loop.
    There isn't a need to catch any errors with a try/catch.

    Move all your variable declarations so they're all together at the top of your main method and remove the following variables:

    colorreturnInputMsg, colorInputMsg, customerColor1, color, customerColor2

    They are not needed.

    Edit:
    Adding another input isnt what you want to do. you just want to loop around to the same line that asks the question.

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

    Default Re: try/catch Help please

    the try/catch is required for all my user inputs, even though i dont need it on this particular example I want to get it working so I can adapt it to other classes in my project

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

    Default Re: try/catch Help please

    Welll... ok then. I would just have the question, checking, and the switch in one for loop and that's it. It just seems like unnecessary code to have the question asked again in a catch.
    You cant just:

    [sudocode]
    Java Code:
    for(3 times)
    {
        ask question;
        check if valid;
        if(valid)
            do things;
        if(notValid)
            do things;
    }
    [/sudocode]
    or do you NEED to do stuff in the catch?

    Edit:
    And I wouldn't ask the question again in a case.
    Last edited by brynpttrsn; 05-07-2012 at 06:53 AM.

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

    Default Re: try/catch Help please

    let me back up.... even though I have the loop in there, if it goes to the loop when I put the correct value it stays in the loop....

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

    Default Re: try/catch Help please

    I would like to catch if there is anything other than alpha string inputted

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

    Default Re: try/catch Help please

    In that case you need to have some custom error checking in your case 2:

    lets work on that before we do any loops.
    (forget about your for loop in your case for now.)

    You can throw Exceptions manually with this line:
    Java Code:
    throw new Exception();
    Exception can be a custom throwable Object or you can just throw the generic Exception() like shown in the previous code.
    this will get you to the catch statement.

    So I suppose you could check the input string with an if like so:
    Java Code:
    if (youreStringHere.matches("[a-zA-Z]*")) {  
        System.out.println("Pure alphabetic string");  
    }
    else
    {  
        System.out.println("Not pure alphabets");  
    }
    Last edited by brynpttrsn; 05-07-2012 at 07:26 AM.

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

    Default Re: try/catch Help please

    disregard last post.... now the error checking doesnt work.... i swear it just did minutes ago... now if i enter the wrong color, and i reenter any color it says that is a valid color.... arrrghhhh x 2

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

    Default Re: try/catch Help please

    What do you want to happen if they input a string with all alpha as opposed to not alpha?
    The way I read your code it will tell the user that whatever they input (as long as its alpha) is a valid input.

    Edit: yeah thats what i thought

    What you want to do is throw an error manually if its not alpha:
    Java Code:
    if(!colorcustomerReturn.matches("[a-zA-Z]*")
    {
        throw new Exception();
    }
    But before you do anything remove the for for now... we will add loops at the end.
    Last edited by brynpttrsn; 05-07-2012 at 07:33 AM.

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

    Default Re: try/catch Help please

    Well if its not Blue, Black, Green, or Gold I need it to reprompt to Case 2... Which asks them the original color question... after 3 tries the system exits

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

    Default Re: try/catch Help please

    you have to be the most patient person ever

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

    Default Re: try/catch Help please

    It can be done really easily at the end no loops for now lol.

Page 2 of 4 FirstFirst 1234 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
  •