title: Java: Try-Catch statement within a Do-While loop
body: <p>I am trying to execute a bit of code that scans for a user input value. This action is contained in a custom method I wrote, named getTriangleDim(); the method reads in the users int value, makes sure it is within a certain range, and then returns the int value that was entered. The method works great and I have no issues with it. </p>
  
  <p>The problem arises when I enter a non-int value for my getTriangleDim() method. It gives me an InputMismatchException error. I have written a try-catch statement into a do-while loop to attempt to fix this issue. But this is the first time I have ever used a try-catch statement, and I am apparently missing something.</p>
  
  <p>Here is the code for the try-catch statement nested within the do-while loop:</p>
  
  <pre><code>//loop to scan for triangle dimension
          boolean bError = true;
          int triangle;
  
          do{
              try {
                  triangle = getTriangleDim();
                  bError=false;
                  }
              catch (Exception e){
                  System.out.println("You did not enter an integer, please enter an integer value");
                  triangle = getTriangleDim();
  
                  }
          }while (bError);
  </code></pre>
  
  <p>if i test it by entering a char value in place of the int, it actually catches the error once, and then prints my "you did not....." statement. But if I re-enter another non-int number, I get a runtime error again that says.......you guessed it........ InputMismatchException error.</p>
  
  <p>The code for my method is here:</p>
  
  <pre><code>//method for scanning triangle dimensions from keyboard
      public static int getTriangleDim(){
          int triangle = 0;
          Scanner keyboard = new Scanner(System.in);
          do{
          System.out.print("Enter a non-zero integer length (+/-1 - +/-16): ");
          triangle = keyboard.nextInt();
          if((!(triangle &lt;= 16 &amp;&amp; triangle &gt;= 1))&amp;&amp;(!(triangle &gt;= -16 &amp;&amp; triangle &lt;= -1)))
              System.out.println("Inpute value outside of range");
          }while((!(triangle &lt;= 16 &amp;&amp; triangle &gt;= 1))&amp;&amp;(!(triangle &gt;= -16 &amp;&amp; triangle &lt;= -1)));
          return triangle;
      }
  </code></pre>
  
  <p>I need the Do-While loop to continue, but I keep getting these errors.</p>