Results 1 to 20 of 20
  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default Need help with exception handling for a string

    I am writing a program that you have to enter either "R", "T", or "C" for some code to execute. I have everything running but cannot figure out how to handle if a different letter is entered or a number.....I want a reenter message to print and do again. I know how to use the numberFormat exception, but don't know how a string exception would be coded. Do I have to make my own? I tried using if statements, but that doesn't work. Heres the if's I had:

    //get input from the user
    System.out.println("Enter customer type (r/c/t): ");
    System.out.println();
    customerType = sc.next();

    //check for non r/t/c entry

    if(!customerType.equalsIgnoreCase("R")){
    if(!customerType.equalsIgnoreCase("T"))
    if(!customerType.equalsIgnoreCase("C"))

    System.out.println("Re-enter");

    }else{

    System.out.println("Enter subtotal: ");
    System.out.println();
    double subtotal = sc.nextDouble();

    this only works for R entry.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    how to handle if a different letter is entered
    Put the asking for the letter and the testing of the letter in a while loop.
    Don't exit the loop unless you get a valid letter or the user asks to quit.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Use a better statement?
    Java Code:
    while(true)
    {
         if(!(myString.equalsIgnoreCase("R")||myString.equalsIgnoreCase("T")||myString.equalsIgnoreCase("C"))
         {
         //Code & way to exit loop
         }
    }
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    But how do I while loop for 3 input letters r/c/t? Make 3 seperate while loops?

  5. #5
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    I tried the OR if statement and couldn't get it to work.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    No, one is enough.
    You need to work on your if test for valid input.
    Your nested ifs are awkward.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I tried the OR if statement and couldn't get it to work.
    Please post your code with the errors or your explanation of what was the problem.

  8. #8
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    This was my if-OR's:

    //check for non r/t/c entry
    if(!customerType.equalsIgnoreCase("R")||!customerT ype.equalsIgnoreCase("T")||!customerType.equalsIgn oreCase("C")){
    //if(!customerType.equalsIgnoreCase("T"))
    // if(!customerType.equalsIgnoreCase("C"))
    System.out.println("Re-enter");
    }else{
    {perform code}

    It compiles and then running and entering r............

    Enter customer type (r/c/t):

    r
    Re-enter
    Enter customer type (r/c/t):

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Java Code:
    if(!customerType.equalsIgnoreCase("R")||!customerT ype.equalsIgnoreCase("T")
    The logic is wrong.
    If customerType is "T" then it is NOT "R"
    The first condition will be true

  10. #10
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    But doesn't it look at all 3 conditions and if the input isn't r or t or c then

    System.out.println("Re-enter");

    ?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    But doesn't it look at all 3 conditions and if the input isn't r or t or c then
    Write down the conditions and the boolean operators.
    Set the variable to a value for testing
    Then write down underneath each condition if it is true or false with that value of the variable.
    Then apply the operators to those true and false values to get the final value for the expression.
    With OR condition, the first true value will stop the rest of the compares.
    With AND condition the first false value will stop the rest

  12. #12
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    I got it to work from Dark's post....changing if(!customerType.equalsIgnoreCase("R")||!customerT ype.equalsIgnoreCase("T")||!customerType.equalsIgn oreCase("C")){
    to... if(!(customerType.equalsIgnoreCase("R")||customerT ype.equalsIgnoreCase("T")||customerType.equalsIgno reCase("C"))){

    worked!
    thanks Dark

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Do you know why Dark's version works?
    Writing boolean expressions takes a bit of study of the logic tables.

  14. #14
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    not yet but am thinking bout it.

  15. #15
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    something to do with the original was looking at !on each expression and was confused? and with !(expression) was not?

  16. #16
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yes(regarding Norm's statement), don't just take the code. Make sure you understand it, otherwise you aren't going to become better at programming. The reason why mine works is because its looking for a true statement first. IE

    With the input of R the check would stop at this statement customerType.equalsIgnoreCase("R"). Since it is true, the next part of the statement is looking to see if its not true.

    !(true) = false;

    If your condition of the if statement is false, then it won't enter the if statement.

    Now if your input was T then it would get to the second or check. Ie: myString.equalsIgnoreCase("R")||myString.equalsIgnoreCase("T")
    Since one of the conditions is met in your or statement, it is true. Resulting in another !(true) statement. This continues for as many conditions you have in your or statement.

    Did that make sense?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  17. #17
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Yes that makes sense...thanks! Java can get a bit confusing sometimes.

  18. #18
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    The hard part about any coding is the logic, and since you determine the logic used its on you how confusing it becomes. There are plenty of articles that can help explain how the logic of each syntax works, so don't be afraid to ask Google. I heard he is a pretty nice fellow. Most of the time you will find someone who had the same problem as you, and it will be explained right there for you so you don't have to wait. If you still have questions after that, then posting for further clarification is encouraged.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  19. #19
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    K, thanks, Dark

  20. #20
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    No problem, if you have any more questions you need answered come back and visit. We don't bite, except for maybe Junky and Daryl, but they don't bite that often.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 10:13 AM
  2. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM
  3. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  4. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM

Posting Permissions

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