Search code examples
javatry-catch-finally

When using try/catch/finally, with error inputmismatch exception, How do I correctly implement a finally block, WITHOUT getting an error


AND YES I have looked at simialr quesions and NO I cannot find an answer to my question...If you have a question about my question or code. PLEASE ASK.

do{
         try {
            System.out.print ("Volume of a cone... V = 1/3(3.14)r^2(h)");
            System.out.println ();
            System.out.print ("Input Radius: ");
            radius = keyBoard.nextDouble ();
            System.out.print ("Input Height: ");
            height = keyBoard.nextDouble ();
         //math
            volume = 0.333 * pie * radius * radius * height;
            System.out.printf ("Volume =   " + volume);
         }//end try
         catch (Exception Error){
            System.out.println ("You Entered the Wrong Data.");
         }
         finally {

            System.out.println ();

            System.out.print ("Do you want to try again?");
            System.out.println ();
            System.out.print ("Input '1' to go again OR any other key to End.: ");
            counter = keyBoard.nextInt ();
         }//end finally

      }while (counter == 1);

Solution

  • problem:

    counter = keyBoard.nextInt ();
    

    After you input a string to the nextDouble it will consume the newLine character by the nextInt() and will result to InputMismatchException because newLine is not an Int.

    solution:

    consume the newLine character in your catch block before asking for input from the user

    sample:

             catch (Exception Error){
                System.out.println ("You Entered the Wrong Data.");
                keyBoard.nextLine();
             }
    

    EDIT:

    boolean tryAgain = false;
        do{
             try {
    
                 if(tryAgain)
                 {
                    System.out.println ();
    
                    System.out.print ("Do you want to try again?");
                    System.out.println ();
                    System.out.print ("Input '1' to go again OR any other key to End.: ");
                    counter = keyBoard.nextInt ();
                    tryAgain = false;
                 }
    
                System.out.print ("Volume of a cone... V = 1/3(3.14)r^2(h)");
                System.out.println ();
                System.out.print ("Input Radius: ");
                radius = keyBoard.nextDouble ();
                System.out.print ("Input Height: ");
                height = keyBoard.nextDouble ();
             //math
                volume = 0.333 * pie * radius * radius * height;
                System.out.printf ("Volume =   " + volume);
                tryAgain = true;
             }//end try
             catch (Exception Error){
                System.out.println ("You Entered the Wrong Data.");
                keyBoard.nextLine();
                tryAgain = true;
             }
    
          }while (counter == 1);