导入CSV文件导入二维String数组 [英] Importing CSV file into 2D String array

查看:177
本文介绍了导入CSV文件导入二维String数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读一个文本文件到一个二维数组。

I have to read a text file into a 2d Array.

我遇到的唯一问题,就是数组的宽窄不一,有9列的最大尺寸。
我不知道有多少行会。

The only problem I am having, is the width of the array varies, with a maximum size of 9 columns. I don't know how many rows there will be.

有些线路将有例如6列,有的还会有9个。

Some lines will have 6 columns for example, and some will have 9.

这是我的CSV文件的一小部分:

here is a small section of my CSV file:

1908,Souths,Easts,Souths,Cumberland,Y,14,12,4000
1909,Souths,Balmain,Souths,Wests,N
1910,Newtown,Souths,Newtown,Wests,Y,4,4,14000
1911,Easts,Glebe,Glebe,Balmain,Y,11,8,20000
1912,Easts,Glebe,Easts,Wests,N
1913,Easts,Newtown,Easts,Wests,N

和这里是我的code到目前为止

and here is my code so far

    import java.io.*;
import java.util.*;

public class ass2 {

    public static void main(String[] args) throws IOException {
        readData();

    }

    public static void readData() throws IOException{
        BufferedReader dataBR = new BufferedReader(new FileReader(new File("nrldata.txt")));
        String line = "";

        ArrayList<String[]> dataArr = new ArrayList<String[]>(); //An ArrayList is used because I don't know how many records are in the file.

        while ((line = dataBR.readLine()) != null) { // Read a single line from the file until there are no more lines to read

            String[] club = new String[9]; // Each club has 3 fields, so we need room for the 3 tokens.

            for (int i = 0; i < 9; i++) { // For each token in the line that we've read:
                String[] value = line.split(",", 9);                
                club[i] = value[i]; // Place the token into the 'i'th "column"
            }

            dataArr.add(club); // Add the "club" info to the list of clubs.
        }

        for (int i = 0; i < dataArr.size(); i++) {
            for (int x = 0; x < dataArr.get(i).length; x++) {
                System.out.printf("dataArr[%d][%d]: ", i, x);
                System.out.println(dataArr.get(i)[x]);
            }
        }
    }

我得到的错误是:

The error I get is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at ass2.readData(ass2.java:23)
at ass2.main(ass2.java:7)

有人可以帮助:(

Can someone please help :'(

感谢您!

推荐答案

问题是与你的内循环。您正在尝试访问,无论有多少个值就行 价值的9个元素。首先,你应该将分配给是内环之前。然后,您需要将循环迭代限制到最小的9和值的长度

The problem is with your inner loop. You are trying to access 9 elements of value regardless of how many values there are on the line. First, you should move the assignment to value to be before the inner loop. Then, you need to limit the loop iterations to the minimum of 9 and the length of value:

String[] value = line.split(",", 9);                
int n = Math.min(value.length, data.length);
for (int i = 0; i < n; i++) { // For each token in the line that we've read:
    data[i] = value[i]; // Place the token into the 'i'th "column"
}

注意数据的尾部元素

这篇关于导入CSV文件导入二维String数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆