Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 63
Like Tree1Likes

Thread: try/catch Help please

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

    Default Re: try/catch Help please

    well with or without loops you got me!

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

    Default Re: try/catch Help please

    Quote Originally Posted by brynpttrsn View Post
    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.
    Might not have caught my Edit from before.

    Is this what you want it to do? or no?
    Last edited by brynpttrsn; 05-07-2012 at 07:44 AM.

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

    Default 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

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

    Default Re: try/catch Help please

    you can just kinda get rid of everything under.
    throw new Exception();
    }
    keep the else

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

    Default 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"); 
    }

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

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

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

    Default Re: try/catch Help please

    Quote Originally Posted by brynpttrsn View Post
    InputDialogue on line 52 also needs to go.
    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.

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

    Default 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

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

    Default Re: try/catch Help please

    Great! We're really close now.
    Quote Originally Posted by brynpttrsn View Post
    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.
    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.

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

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

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

    Default Re: try/catch Help please

    These lines:
    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";
    need to be moved below your booleans and int declaration.

    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.

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

    Default Re: try/catch Help please

    got it, sorry had edited the previous post.... caught it right after i reposted it

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

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

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

    Default 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:
    Java Code:
    for (int i=1; i <= 3 && returnFlag == 2; i++)
    You might get a initialization error while compiling.
    To fix that replace
    Java Code:
    int returnFlag;
    with
    Java Code:
    int returnFlag = 2;
    (You have to do that anyway so that the for loop executes)

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

    Default 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:
    Java Code:
    int foo;
    boolean boolStuff;
    Initializing means this:
    Java Code:
    a = 0;
    boolStuff = true;
    reading your last post now...

    Edit:
    Where are your {} for your for loop?
    Last edited by brynpttrsn; 05-07-2012 at 09:10 AM.

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

    Default Re: try/catch Help please

    tracking corrected

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

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

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

    Default 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 {}

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

    Default Re: try/catch Help please

    Java Code:
    {            for (int i=1; i <= 3 && returnFlag == 2; i++)  
                   
                //initialize color input
    Should look like:
    Java Code:
     
    for (int i=1; i <= 3 && returnFlag == 2; i++)  
    { 
                //initialize color input

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

    Default Re: try/catch Help please

    Ill attribute that mistake to lack of sleep! Thanks man that did it... fully functional!

Page 3 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
  •