当我尝试将整数文本文件读入 int[] 时出现 NumberFormatException

NumberFormatException when I try to read text file of integers into a int[]

我很困惑。我正在尝试将文本文件的 10 行上的 10 个整数转换为 int[]。我尝试了几种不同的方法,但我最近的尝试是在文件的每一行的 BufferedReader.readLine() 上使用 for 循环和 parseInt。这每次都会返回 NumberFormatException。


public class InversionCounter {

    static int[] fileToArray(String filename) {
        File file = new File(filename);
        try ( BufferedReader br = new BufferedReader(new FileReader(file))) {
        int numOfLine = Integer.parseInt(br.readLine());
        int[] ourArray = new int[10];

        for (int i = 0; i < ourArray.length; i++)  {
            ourArray[i] = numOfLine;
            numOfLine = Integer.parseInt(br.readLine());
        }


        return ourArray;

        }  catch (NumberFormatException e) {
        System.out.println("NumberFormatException");
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } catch (IOException e) {
            System.out.println("IO Exception");
        }

        return null;
    }

    public static void main(String[] args) throws IOException {
        fileToArray("/home/paris/coolfile");

    }



        }

错误:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.base/java.lang.Integer.parseInt(Integer.java:614)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at InversionCounter.fileToArray(InversionCounter.java:16)
    at InversionCounter.main(InversionCounter.java:30)

文件就是这样:

45
67
87
76
7
4
5
23
5675
3

当文件的行被读取到这里时出现问题:

int numOfLine = Integer.parseInt(br.readLine());

引用 BufferedReader.readLine()

的 javadoc

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached without reading any characters.

你需要在解析之前测试读取的行是否为空,否则Integer.parseInt()会抛出一个NumberFormatException.

这是一个简单的修复:

String line = br.readLine();
if (line == null) {
    break;
}
int numOfLine = Integer.parseInt(line);

更好的方法是不假设一定数量的输入,而是一直读取行直到文件末尾(即直到行为空),然后将它们添加到 List<Integer>.

文件中只有 10 行,但您的 readLine 指针正在尝试读取第 11 行。发生这种情况是因为您在循环外读取了一行,现在试图在循环中读取 10 行。以下代码应该有效:

public class InversionCounter {
    static int[] fileToArray(String filename) {
        File file = new File(filename);
        try ( BufferedReader br = new BufferedReader(new FileReader(file))) {
        int numOfLine = Integer.parseInt(br.readLine());
        int[] ourArray = new int[10];

        for (int i = 0; i < (ourArray.length - 1); i++)  {
            ourArray[i] = numOfLine;
            numOfLine = Integer.parseInt(br.readLine());
        }


        return ourArray;

        }  catch (NumberFormatException e) {
        System.out.println("NumberFormatException");
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } catch (IOException e) {
            System.out.println("IO Exception");
        }

        return null;
    }

    public static void main(String[] args) throws IOException {
        fileToArray("/home/paris/coolfile");

    }


}

P.S:另外,如果有多余的空格,你应该trim这一行。