4

我必须将文本文件读入二维数组。

我遇到的唯一问题是数组的宽度不同,最大大小为 9 列。我不知道会有多少行。

例如,有些行有 6 列,有些行有 9 列。

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

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

到目前为止,这是我的代码

    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]);
            }
        }
    }

我得到的错误是:

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

有人可以帮忙吗:'(

谢谢!

4

4 回答 4

11

您可以使用OpenCSV读取 CSV 文件。

// Read all
CSVReader csvReader = new CSVReader(new FileReader(new File("nrldata.txt")));
List<String[]> list = csvReader.readAll();

// Convert to 2D array
String[][] dataArr = new String[list.size()][];
dataArr = list.toArray(dataArr);
于 2013-05-28T05:00:57.653 回答
3

问题在于您的内部循环。value无论在线上有多少值,您都试图访问 9 个元素。首先,您应该将分配移动到value内部循环之前。然后,您需要将循环迭代限制为 9 的最小值和 的长度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"
}

请注意datawill的尾随元素null

于 2013-05-28T04:48:58.137 回答
1

您收到错误消息,因为您尝试在仅包含 6 的行上访问第 7 个令牌(索引 6)。替换为:

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

有了这个:

String[] value = line.spkit(",", 9);   // Split the line into max. 9 tokens
for (int i = 0; i < value.length; i++) {
    data[i] = value[i];   // Add each token to data[]
}

事实上,你可以用这个单线替换整个while-loop身体:

dataArr.add(Arrays.copyOf(line.split(",", 9), 9));

另请参阅这个简短的演示

于 2013-05-28T04:52:11.780 回答
0

您可以使用ArrayListof而不是数组List。由于 List 是可动态增长的,因此您也不需要考虑它的大小。

List<List<String>> dataArr = new ArrayList<List<String>>();

while ((line = dataBR.readLine()) != null){ 
        for (int i = 0; i < 9; i++) 
            dataArr.add(Arrays.asList(line.split(",", 9)));                
}
于 2013-05-28T04:46:28.583 回答