当我尝试将整数的文本文件读取为int []时,出现NumberFormatException

问题描述

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

CREATE TABLE avg_yearly_currencies_used (
    id serial PRIMARY KEY,year date[],CONSTRAINT first_jan_check CHECK (check_date(year))
);

错误


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)

解决方法

在此处读取文件的行时会发生问题:

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

引用BufferedReader.readLine()的Javadoc

返回:一个包含行内容的字符串,不包含任何行终止符,;如果到达流的末尾,则为null ,但不读取任何字符。

您需要先分析读取的行是否为空,然后解析,否则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:此外,如果有多余的空格,您应该修剪线条。