Java InputMismatchException 与 Scanner 循环

标签 java swing exception infinite-loop inputmismatchexception

首先,这是我的类作业之一。我理解教授试图通过这项作业解释的所有概念,但我遇到了一个愚蠢的问题,它不断循环,并且不知道如何阻止它。

我们需要创建自己的异常类,并在创建时间(具有小时、分钟和秒)对象时在特定实例中使用抛出它们。但是,我自己的异常类没有问题,我遇到了连续执行 InputMismatchException 的问题。

此类通过 Scanner 对象读取文件,该对象有几行,每行由三个整数组成,表示军事格式的时间(例如 20 15 33)是晚上 8:15:33。但是一旦存在无效标记(例如二十 15 33 ,其中二十不是整数),它就不会停止 InputMismatchException catch block 。

这是代码:

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

这是示例输出:

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

正如您所看到的,它会在 InputMismatchException 的 catch block 内一遍又一遍地循环“使用了错误的格式”...我无法找到一种方法让它停止并继续读取文件。

任何解释解决方案的帮助将不胜感激,谢谢!

最佳答案

问题在于 while 循环的条件不是输入是否有效,而是文件中是否存在下一行。如果您无法在 while 循环之前验证输入,则应该检查 break 语句。

关于Java InputMismatchException 与 Scanner 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27297752/

相关文章:

java - 如何在 Java for Android (Eclipse) 中插入 swf 动画 (flash)

java - 如何在 Java 中生成随机颜色?

c++ - 在抛出 std::exception 实例后调用终止

java - 如何获取 MBean 绑定(bind)类实例

java - 什么是 NullPointerException,我该如何解决?

Java SWT - 将按钮添加到 shell

java - 如何制作JPanel的背景渐变

java - 如何在 Java 中调整窗口大小而不影响尺寸?

java - 反编译.class文件后如何解决语法错误

android - 我可以在 Android 上的 JNI 库中使用 C++ 异常吗?