Integer.parseInt 错误:java.lang.Integer.parseInt(来源不明)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18777522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:57:07  来源:igfitidea点击:

Integer.parseInt Error : java.lang.Integer.parseInt(Unknown Source)

java

提问by user2774643

I'm working on a Java project to Add each Integer to the one in the next line until there's no lines to read in the file. So to be able to add it I have to use Integer.parseInt(...) to the line then add it. P.S : the for loop will just skip two lines which contain the header of the file. And all the string are refering to numbers which Integer.parseInt() accepts.

我正在开发一个 Java 项目,将每个整数添加到下一行中的一个,直到文件中没有要读取的行。所以为了能够添加它,我必须使用 Integer.parseInt(...) 到该行然后添加它。PS:for循环只会跳过包含文件头的两行。并且所有的字符串都是指 Integer.parseInt() 接受的数字。

Here's the full exception error :

这是完整的异常错误:

Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at prog.Result(prog.java:93)
at prog.main(prog.java:56)

The code resulting in the exception is :

导致异常的代码是:

public static void Result() throws IOException
    {
        FileReader fileReader = new FileReader(dir+"/"+log_file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i;
        for (i=0;i<=2;++i)
        {
            bufferedReader.readLine();
        }
        int result =0;
        while (bufferedReader.readLine() != null)
        {
            result += Integer.parseInt(bufferedReader.readLine());

        }
        System.out.println("The Result Is : " + result);

    }

采纳答案by Mark Elliot

This block is actually reading twolines, not one.

这个块实际上是在读取行,而不是一行。

while (bufferedReader.readLine() != null)
{
    result += Integer.parseInt(bufferedReader.readLine());
}

The error occurs when the last line is read in the whilecondition check, and the inner part of the block will then read null, since there are no more lines to be read.

while条件检查中读取最后一行时会发生错误,然后将读取块的内部部分null,因为没有更多行要读取。

It's idiomatic to write loops like this as:

像这样写循环是惯用的:

String line;
while ((line = bufferedReader.readLine()) != null)
{
    result += Integer.parseInt(line);
}

回答by DaoWen

I think this is your problem:

我认为这是你的问题:

    while (bufferedReader.readLine() != null)
    {
        result += Integer.parseInt(bufferedReader.readLine());

    }

You're calling readLine()twice there. You should store the initial result in a variable, and the reuse that result in the parseInt()call.

你在readLine()那里打了两次电话。您应该将初始结果存储在一个变量中,并在parseInt()调用中重复使用该结果。

回答by Ha Nguyen

First: you should check data in your file to ensure that all lines are number.

首先:您应该检查文件中的数据以确保所有行都是数字。

Second: you should try catch at the line as below

第二:你应该尝试在下面的行中捕捉

try {
    result += Integer.parseInt(bufferedReader.readLine());
} catch(Exception ex)