0

The first loop in the second method works perfectly. when the user inputs characters instead of numbers the InputMismatchException is caught and the user is prompted to re-enter numbers only in order to continue. The second loop fails: If characters are entered for the next question, the InputMismatchException is caught and "...please try again" is displayed but then the program continues on without forcing the user to re-enter numbers. Is it possible to have more than 1 do/while, try/catch for an InputMismatchException within 1 method? I have no idea what I am doing wrong. I think I am right in theory but obviously not! How can I fix this without separating into different methods (because then my method for calculations won't work)?

           {
             Scanner input = new Scanner( System.in );
             boolean continueLoop = true;


              do{
                System.out.print("\nPlease enter weight");
                   try{

                      stone = input.nextInt();

                         while (...)
                         {
                         System.out.print("...");
                         stone = input.nextInt();
                         }
                         continueLoop = false;
                      }
                      catch ( InputMismatchException inputMismatchException)
                      {
                      System.err.printf( "\nException: %s\n", 
                      inputMismatchException );
                      input.nextLine();
                      System.out.println("...Please try again.\n" );
                      }


                 }while ( continueLoop ); 

               do{
                 System.out.print("...");
                    try{

                       pounds = input.nextDouble();


                          while (...)
                          {
                          System.out.print("...");
                          pounds = input.nextDouble();
                          }
                          continueLoop = false; 
                       }
                    catch ( InputMismatchException inputMismatchException)
                        {
                        System.err.printf( "\nException: %s\n", 
                        inputMismatchException );
                        input.nextLine();
                        System.out.println("..." );
                        }

                 }while ( continueLoop ); //code continues more methods...
  • 1
    When the first loop starts, `continueLoop` is true because that’s how it’s initialized. But when the second loop starts, `continueLoop` is false, because that was what caused the first loop to terminate. – VGR Oct 23 '19 at 02:29
  • You need to reset the `continueLoop ` value to `true` before the subsequent loops – Scary Wombat Oct 23 '19 at 02:30
  • Also, that `input.nextDouble();` inside the second `catch` is going to burn you bad. It should probably be `input.nextLine();` like the other one, or just be omitted altogether. – Dawood ibn Kareem Oct 23 '19 at 02:31
  • I tried to reset boolean continueLoop = true; after the first loop and now its giving me an error relating to my main method which also has boolean continueLoop = true;. It says its already been defined in main. I've edited the code above to include my main method. This is so confusing to me. (I'm new to Java obviously) – Julie Cohen Oct 23 '19 at 03:09
  • @Dawood ibn Kareem Thanks! I've changed it to nextLine – Julie Cohen Oct 23 '19 at 03:26

1 Answers1

0

Thanks to quick responses from VGR, Scary Wombat and Dawood ibn Kareem I now understand the solution. I reset 'boolean continueLoop = true' before the next loop but got an error. After more research (which the responses put me on the right path to) I finally figured out that I reset by entering 'continueLoop = true'!! Big problem solved and a lot learned. I spent so much time on this! I hate java. but I love Java.