Results 1 to 9 of 9

Thread: try catch help

  1. #1
    vividcooper is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Cool try catch help

    Hi
    I have a situation where i m displaying a menu,then allowing the user to enter an option from the menu (example press1),
    then using a switch statement (with the cases.)
    In case 5 I am using try & catch so if the user enters a string instead of an integer, the program tell him that it is invalid and breaks to the first loop.

    My problem:
    after entering a wrong data entry (example entering "K" or any other letter) (in case 5), the try catch block is executing but as soon as this breaks, the program is displaying the menu again BUT is skipping the option after the display menu and is giving the error message "to enter another number".....instead of allowing the user to enter the option first
    what am I doing wrong?

    quit = false;

    do{
    System.out.println("\n\n**** BANK ACCOUNTS ****");
    System.out.println(" ");
    System.out.println("PRESS 1 to ADD a Savings Account");
    System.out.println("PRESS 2 to ADD a Current Account");
    System.out.println("PRESS 3 to SEARCH for an Account");
    System.out.println("PRESS 4 to DELETE an existing Account");
    System.out.println("PRESS 5 to EDIT information on a particular Account");
    System.out.println("PRESS 6 to LIST available information on all Accounts");
    System.out.println("PRESS 0 to EXIT the menu");
    System.out.println("\n \n");

    final String OPT2 = "[0-6]";
    String option2;
    int userop2;



    System.out.print("Please enter your option: ");
    option2 = sc.next();

    //validating user input

    if(!option2.matches(OPT2)){
    do{
    System.out.print("Wrong input!!!\nPlease re-enter your option again: ");
    option2 = sc.next();
    }while(!option2.matches(OPT2));
    }


    userop2=Integer.parseInt(option2);//typecasting String to integer because String did not work in Switch statement

    if(userop2 == 0){
    quit = true;}


    switch(userop2){

    case 1 : ...
    case 2 : ...
    case 3
    case 4
    case 5 : System.out.println("\n \n");
    System.out.println("EDITING information on a chosen Account");
    System.out.println();

    accountnumber = 0;


    /*allowing the user to input the account number as an intger and validating user input
    with the use of Exception Handling*/
    try{
    System.out.print("Enter Account Number of the account you wish to edit: ");
    accountnumber = sc.nextInt();

    }
    catch(Exception exc){
    System.out.println("Invalid Entry\n");
    break;
    }



    }while(quit == false);

    thank you

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Post your code in CODE tags as it's really difficult to see the flow without proper formatting.

  3. #3
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    use if statement. switch sucks.

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by Aseem View Post
    use if statement. switch sucks.
    i kinda agree switch is a headache sometimes.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Sorry, but switch makes things significantly easier to read than a series of if/else if statements.

    In this case it is most certainly the correct thing to use and if, in a code review, I saw the if/else version of this I would point out that it ought to be a switch.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I have to go with Tolls on this... python code is a nightmare because of its elif statements - the saving grace is that it is pretty simple, as its a simple elif check==0: do next set of code - but that leads me into the failings of indentation-driven programming etc.

    switch is extraordinarily useful - sometimes I wish it would let you use Objects. If you ever have 100 different possibilities for an int value, and use an if-else-if, SOMEONE will find it and kill you for it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    i never use switch. never liked switch. For me, if else is easier to read.

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Then you've never written code with a random integer for creating a hundred different kinds of objects based solely on an int value. (don't ask, it was horrible!) Switch is something that I see as necessary in a language, and as scripting languages don't have it most of the time, I avoid them.

    You may notice a pattern here - those advocating switch appear to be more experienced, at least with more posts, (and I know I and Tolls both have experience - I can tell from his other posts, and I know my abilities) while those who dislike it don't appear to have as much experience. No offence meant, we all start somewhere, but it appears to be an acquired taste. You'll eventually find you need it. If you were writing Pascal, you'd use the equivalent of switch so often you'd hate if!
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Quote Originally Posted by Singing Boyo View Post
    switch is extraordinarily useful - sometimes I wish it would let you use Objects.
    I don't wuite know how it would work with objects, though. What would it actually look like?

Similar Threads

  1. 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
  2. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 05:39 PM
  3. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  4. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  5. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 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
  •