0

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.

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.

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.

This is the 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.");
        }
    }
}
}

And this is sample output:

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

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.

Any help would be appreciated in explaining a solution, thanks!

1 Answers1

1

The issue is that the while loop's condition isn't on whether the input is valid, but instead if there exists a next line in the file. You should check out the break statement if you don't have the ability to validate input before the while loop.

MiiinimalLogic
  • 820
  • 6
  • 11
  • I see what you're saying. Correct me if I'm misunderstanding what you mean but I have tried placing a break after the catch for IllegalMismatchException such that it would stop, but then the program quits there and no longer reads the rest of the file, which is problematic if there are more lines to be read. – mugiwaragirl Dec 04 '14 at 15:38
  • Did you put the break inside the catch brackets or outside? – MiiinimalLogic Dec 04 '14 at 15:41
  • Inside the catch brackets, after the System.err.println("Incorrect format used"); – mugiwaragirl Dec 04 '14 at 15:49
  • I ran your code in the debugger (without the hour, minute, second exceptions) with a `break;` after the `System.err.println("Incorrect format used");` and I cannot recreate the error. Try refreshing or cleaning your project after adding the `break;` statement. – MiiinimalLogic Dec 05 '14 at 04:51