繁体   English   中英

如何读取 CSV 文件的内容并将其放入 Java 中的二维数组中?

[英]How to read the contents of a CSV file and put it into a 2D array in Java?

我只是想问一下如何读取 CSV 文件的内容并将其放入二维数组中。

我的 CSV 文件的内容每行包含 7 列(以逗号分隔的“,”)。 另一方面,行由破折号/连字符“-”分隔。 但请注意,这些行仍然在同一行上(它们只是在有分隔 7 列(每行)的破折号/连字符时才被识别。

(CSV 文件中出现的内容是用户输入的,即用户为每行的列输入 7 个值。)

CSV 文件内容

我的问题是,每当用户完成输入第一行的值时,我都会得到一个 java.lang.ArrayIndexOutOfBoundsException: 7 来自我的二维数组声明的 7 列(我计划在其中插入 CSV 文件的值)那在我的 readCustomerCSV 函数中。

function 的具体行是这样的: read2DString[read2DStringIndex][g] = fromfile[g];

这是我的源代码:

public void writeCustomerCSV() {  // everything in this snippet code works fine(it creates a CSV file which stores the inputs of the user)
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
        StringBuilder sb = new StringBuilder();
        int y;
        for (int x = 0; x < itemTo2D.length; x++) {
            for (y = 0; y < itemTo2D[0].length; y++) {
                if (itemTo2D[x] != null) {
                    sb.append(itemTo2D[x][y]);
                    sb.append(",");
                }
            }
            sb.append("-");  //separation for rows
            sb.append(",");  // separation for columns
        }
        bw.write(sb.toString());
        bw.close();
    } catch (Exception ex) {

    }
}

public void readCustomerCSV() {  // reads the contents of the CSV file     *having issues with ArrayINdexOutofBounds
    String[] fromfile = {};   // 1d string for getting the columns(7 columns) of the CSV file
    String[][] read2DString = new String[10][7];   // 2D array where the contents of the CSV file will be inserted (can only get 10 unique values of 7 columns)
    try {
        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
        String line;
        while ((line = br.readLine()) != null) {
            fromfile = line.split(",");  //separates the columns by a comma
        }
    } catch (Exception ex) {

    }

    for (int g = 0; g < fromfile.length; g++) {
        read2DString[read2DStringIndex][g] = fromfile[g];  // the ArrayIndexOutofBounds is here (inserts the values of the 2D array(by row))
        //   System.out.print(fromfile[g] + " ");
        if (fromfile[g].equals("-")) {     //if there is a presence of a dash, it increments the read2DStringINdex (row index) of the 2D array
            read2DStringIndex++;
        }
    }
}

我的代码中是否有任何遗漏或者我的方法不够好?

在将数据解析为 2D 数组之前,您的代码会遍历数据的 1D 数组,每个最终行由破折号分隔。 当您进入 for 循环时,您的代码将破折号视为read2DString[read2DStringIndex][g] = fromfile[g]行中的第 8 列,因为 g 表示索引为 7。只有在此之后,您的代码才会增加排。 为了解决这个问题,您需要将 if 语句放在此行之前,然后使用“继续;” 跳到循环的下一个迭代来绕过这个问题。

当您增加行时,您还必须将列重置为 0,因此请使用read2DString[read2DStringIndex][g] = fromfile[g]而不是 read2DString read2DString[read2DStringIndex][g%8] = fromfile[g] 模数运算符 (%) 将为您提供除法后的余数,在这种情况下,它是在除以已完成行的长度后的正确列号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM