簡體   English   中英

將CSV文件導入2D字符串數組

[英]Importing CSV file into 2D String array

我必須將文本文件讀入2d數組。

我唯一的問題是,數組的寬度是變化的,最大大小為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)

有人可以幫忙:'(

謝謝!

您可以使用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);

問題在於你的內循環。 無論線上有多少值,您都試圖訪問9個value元素。 首先,您應該將賦值移動到內部循環之前的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"
}

請注意, data的尾隨元素將為null

您收到錯誤,因為您嘗試訪問僅包含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));

另見這個簡短的演示

您可以使用List ArrayList而不是數組。 由於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)));                
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM