title: Java InputMismatchException loops with Scanner
body: <p>first off this is one of my assignments for a class. I understand all the concepts my professor was trying to explain with this assignment but I am having a silly issue with it continuously looping, and cannot figure out how to stop it.</p>
  
  <p>We were required to create our own Exception classes and use throw them at specific instances when a Time (with an hour, minute, and second) object is created. However, I'm not having a problem with my own exception classes, I am having an issue with the InputMismatchException continuously being executed.</p>
  
  <p>This class reads a file through a Scanner object which has a few lines consisting of three integers on each line to represent a time in military format (ex. 20 15 33) is 8:15:33 P.M. but once there is an invalid token (ex. twenty 15 33 , where twenty is not an integer) it just won't stop the InputMismatchException catch block.</p>
  
  <p>This is the code:</p>
  
  <pre><code>public class TimeTestNew
  {
  
  public static void main (String[] args)throws IllegalHourException,
          IllegalMinuteException,
          IllegalSecondException,
          FileNotFoundException,
          NumberFormatException
  {
      boolean fileFound = false;
  
      while(!fileFound)
      {
          String input = JOptionPane.showInputDialog("Enter filename: " );
  
          try
          {
              Scanner in = new Scanner(new File(input));
              fileFound = true;
  
              while(in.hasNextLine())
              {
                  int hour = 0, minute = 0, second = 0;
                  try
                  {
                      hour = in.nextInt();
                      minute = in.nextInt();
                      second = in.nextInt();
                      Time theTime = new Time(hour,minute,second);
                      System.out.println(theTime);
                  }
                  catch(IllegalHourException e)
                  {
                      System.err.println("Sorry, \"" + e.value 
                              + "\" is an invalid hour.");
                  }
                  catch(IllegalMinuteException e)
                  {
                      System.err.println("Sorry, \"" + e.value 
                              + "\" is an invalid minute.");
                  }
                  catch(IllegalSecondException e)
                  {
                      System.err.println("Sorry, \"" + e.value 
                              + "\" is an invalid second.");
                  }
                  catch(NumberFormatException | InputMismatchException e)
                  {
                      System.err.println("Incorrect format used.");
                      // this is what keeps getting executed
  
                  }
  
              }
  
              in.close();
  
          }
          catch (FileNotFoundException e)
          {
              System.err.println("ERROR: \"" + input + "\" not found\n"
                      + "Please enter a valid filename.");
          }
      }
  }
  }
  </code></pre>
  
  <p>And this is sample output:</p>
  
  <pre><code>12:12:12 P.M.
  Sorry, "24" is an invalid hour.
  1:02:03 A.M.
  1:13:13 P.M.
  Sorry, "90" is an invalid minute.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  Incorrect format used.
  //...and this goes on forever
  </code></pre>
  
  <p>As you can see, it will loop that "Incorrect format used" inside the catch block for InputMismatchException over and over and over...I cannot figure out a way to get it to stop and continue reading the file.</p>
  
  <p>Any help would be appreciated in explaining a solution, thanks!</p>