繁体   English   中英

Java InputMismatchException循环扫描程序

[英]Java InputMismatchException loops with Scanner

首先,这是我的一堂课。 我理解我的教授试图用此作业解释的所有概念,但是我却遇到了一个愚蠢的问题,它不断循环,无法弄清楚如何停止它。

我们需要创建自己的Exception类,并在创建Time(带有时,分和秒)对象时在特定实例上使用它们。 但是,我自己的异常类没有问题,InputMismatchException一直在执行中。

此类通过Scanner对象读取文件,该对象包含几行,每行由3个整数组成,以军事格式表示时间(例如20 15 33)是晚上8:15:33,但是一旦存在无效令牌(例如20 15 33,其中20不是整数),它不会停止InputMismatchException catch块。

这是代码:

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块内的“使用了错误格式” ...我无法找出一种方法来停止它并继续读取文件。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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