从CSV文件中填写double [] []

时间:2018-01-29 04:12:22

标签: java arrays csv

我有这个CSV文件:

World Development Indicators
Number of countries,4
Country Name,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014
Bangladesh,6.28776238,13.20573922,23.46762823,30.16828408,34.35334451,44.94535882,55.19256723,62.82023906,74.42964608,80.03535051
"Bahamas, The",69.21279415,75.37855087,109.340767,102.7875065,101.2186453,118.8292307,81.5628489,80.65383375,76.05187427,82.29635806
Brazil,46.31418452,53.11025849,63.67475185,78.5549801,87.54187651,100.8810115,119.0023853,125.0018521,135.3050481,138.9514906  
Germany,94.55486999,102.2828888,115.1403608,126.5575074,126.2280577,106.4836959,109.6595675,111.5940398,120.9211651,120.4201855

我正在尝试将国家/地区的数据(加倍一次)存储到矩阵中(double [] [])。这是我到目前为止的代码:

public double[][] getParsedTable() throws IOException {
    double[][] table = new double[4][10];
    String row;
    int indexRow = 0;
    int indexColumn = 0;
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    br.readLine();
    br.readLine();
    String line = br.readLine();
    while(line != null && !line.isEmpty()){
        line = br.readLine();
        String[] array = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
        for(int i = 1; i < array.length; i++){
            table[indexRow][indexColumn] = Double.parseDouble(array[i]);
            indexColumn++;
        }
        indexColumn = 0;
        indexRow++;
    }
    System.out.print(Arrays.deepToString(table));
    return table;
}

我得到了一个恐怖:NullPointerException at:

String[] array = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);

我无法弄清楚原因。我尝试了不同的组合。似乎没什么用。它似乎从CSV文件中获取数字并存储它们,但是当我打电话时:

System.out.print(Arrays.deepToString(table));

它不打印任何东西,因此我无法检查它是否正确存储。你能告诉我:1. Why I am getting an error. 2. Why System.out.println does not print out an array.谢谢

3 个答案:

答案 0 :(得分:1)

Arrays.deepToString是错误的。您正在传递一组基元。传入时,传入double [] []。这是对象[],其中对象是double [],因此它将尝试打印double []对象,而不是打印双打。

一种解决方案是创建一个数组Double [] []。

更改

 double[][] table = new double[4][10];

 Double[][] table = new Double[4][10];

Autoboxing会将每个double转换为Double。由于Double是一个对象而不是一个原始对象,因此deepToString将单独打印出每个Double。如果您读取了deepToString的javadoc,它会解释它在引用类型的数组上递归操作,而不是在原始数组上。

如果你想坚持使用double [] []

for (int i = 0; i < table.length; i++) {
    for (int j = 0; j < table[i].length; j++) {
        System.out.print(table[i][j]);
        System.out.print(' ');
    }
    System.out.println();
}

答案 1 :(得分:1)

在csv中,前3行不是真实国家的数据。因此,在line-4循环开始之前阅读while

在while循环中,首先完成line字符串的处理。例如:正则表达式检查&amp;将拆分数据分配到table

然后只在next line循环结束时读入while,以便在下一次迭代中处理。

随意尝试:

public double[][] getParsedTable() throws IOException {
    double[][] table = new double[4][10];
    int indexRow = 0;
    int indexColumn = 0;

    // check whether you need to handle any exception for this
    BufferedReader br = new BufferedReader(new FileReader(fileName));

    String line = null;

    try {
        // line 1-3 are not real country's data
        br.readLine();
        br.readLine();
        br.readLine();

        // first country data begin at line 4
        line = br.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }

    while (line != null && !line.isEmpty()) {            
        String[] array = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);

        for (int i = 1; i < array.length; i++) {
            table[indexRow][indexColumn] = Double.parseDouble(array[i]);
            indexColumn++;
        }

        indexColumn = 0;
        indexRow++;

        // read next line only at end of loop, not beginning of loop
        // line is ready to be processed at next iteration
        try {
            line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    System.out.print(Arrays.deepToString(table));
    return table;
}

答案 2 :(得分:1)

如果我们假设某个国家/地区的名称不包含数字和国家/地区名称,并且数字将仅以逗号分隔,则可以在不使用正则表达式的情况下执行以下操作。我已经更改了文件读取,因为它可能会遇到问题。

public double[][] getParsedTable() throws IOException {
    double[][] table = new double[4][10];
    int indexRow = 0;
    int indexColumn = 0;
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    br.readLine(); // ignore first line
    br.readLine(); // ignore second line
    br.readLine(); // ignore third line (contains title)
    String line;
    while (true) {
        line = br.readLine();
        if (line == null) break; // end of file reading

        int index = 0;
        while (true) {
            index = line.indexOf(",", index) + 1;
            if (Character.isDigit(line.charAt(index))) {
                break;
            }
        }

        // from index, line is expected to contain comma separated numbers
        String[] array = line.substring(index).split(",");
        for (int i = 0; i < array.length; i++) {
            table[indexRow][indexColumn] = Double.parseDouble(array[i]);
            indexColumn++;
        }
        indexColumn = 0;
        indexRow++;
    }
    System.out.print(Arrays.deepToString(table));
    return table;
}