简体   繁体   中英

Using a try/Catch in a Do While loop

I'm trying to use a try-catch statement inside of a do-while loop. The logic seems to work, but the do loop is not looping over and over. If it goes into the catch it seems to just break out of the do loop.

private boolean madeChoice = false;

public void whatToDo(){
    System.out.println("");
    System.out.println("");
    System.out.println("=====================================");
    System.out.println("Welcome");
    System.out.println("=====================================\n");
    System.out.println("What would you like to do today? Your choices are: \n");
    choices();

    do{
        try{
            System.out.println("");
            System.out.println("Please enter your choice");
            int numberEntered = keyboard.nextInt();

            if(numberEntered > 3 || numberEntered < 1){
                System.out.println("-----------------------------------------------------------");
                System.out.println("That is not a choice. Please choose from the following: \n");
                choices();
                numberEntered = keyboard.nextInt();
               
            }else{
                System.out.println("That is a good CHOICE");
                madeChoice = true;
            }
          
        }catch (InputMismatchException e){
            System.out.println("-----------------------------------------------------------");
            System.out.println("This is not a number. Please choose from the following: \n");
            choices();
            
        }

    }while(!madeChoice);

}

private void choices(){
    System.out.println("1. Add a new set into your account");
    System.out.println("2. See all the sets in your account");
    System.out.println("3. Exit the Account Manger.");
}

Add keyboard.nextLine() before continue :

           catch (InputMismatchException e){
                System.out.println("-----------------------------------------------------------");
                System.out.println("This is not a number. Please choose from the following: \n");
                choices();
                keyboard.nextLine();
                continue;
                // numberEntered = keyboard.nextInt();
            }

may be you can update your code to something like this, which seems to work for me:

private boolean madeChoice = false;
private static boolean isMissMatchException = false ;
private static boolean isInvalidInput = false ;

public void whatToDo(){
    if(Test.isMissMatchException) 
    {
         System.out.println("-----------------------------------------------------------");
         System.out.println("This is not a number. Please choose from the following: \n");
         Test.isMissMatchException = false ;
    } else if (Test.isInvalidInput) 
    {
        System.out.println("-----------------------------------------------------------");
        System.out.println("That is not a choice. Please choose from the following: \n");
        Test.isInvalidInput = false ;
    } else {            
        System.out.println("");
        System.out.println("");
        System.out.println("=====================================");
        System.out.println("Welcome");
        System.out.println("=====================================\n");
        System.out.println("What would you like to do today? Your choices are: \n");
    }
    choices();
    
    do{
        try{
            System.out.println("");
            System.out.println("Please enter your choice");
            
            @SuppressWarnings("resource")
            int numberEntered = new Scanner(System.in).nextInt();

            if(numberEntered > 3 || numberEntered < 1){
                Test.isInvalidInput = true ;
                this.whatToDo();
               
            }else{
                System.out.println("That is a good CHOICE");
                madeChoice = true;
            }
          
        }catch (InputMismatchException e){
            Test.isMissMatchException = true ;
            this.whatToDo();
        }

    }while(madeChoice);

}

private void choices(){
    System.out.println("1. Add a new set into your account");
    System.out.println("2. See all the sets in your account");
    System.out.println("3. Exit the Account Manger.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM