输入文件验证数据

INPUT FILE VALIDAT DATA

我好像想不出一个问题。这是我正在尝试打开并读取 .txt 文件的内容。如果文件中的某一行数据有问题,请跳过它并继续阅读文件

我找不到跳过不包含有效值的行并继续读取文件的方法。这是我的代码。

int theValue = 0;

try {
        Scanner input = new Scanner(file);
        while (input.hasNextLine()) {
            String value = input.next();

            theValue = Integer.parseInt(value);



        }
        input.close();


    } catch (IllegalArgumentException error) {
        System.out.println(error.getMessage());
    }
}

提前感谢所有帮助。

好吧,也许不是最好的主意,但一个简单的解决方案是将 parseInt 封装在 try/catch:

int theValue = 0;

try {
        Scanner input = new Scanner(file);
        while (input.hasNextLine()) {
            String value = input.next();

            try{
                theValue = Integer.parseInt(value);
            }catch (Exception e){
                //Just ignore it and carry on.
            }



        }
        input.close();


    } catch (IllegalArgumentException error) {
        System.out.println(error.getMessage());
    }
}