简体   繁体   中英

Java InputMismatchException loops with Scanner

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 PM 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM