java - ArrayIndexOutOfBoundsException - 解析 csv 文件时

标签 java csv opencsv

我想转换一个 csv 文件。我的文件看起来像这样:

enter image description here

我正在使用 opencsv 库来解析我的 csv。这是我解析文件的 run 方法:

public void run() throws Exception {


        CSVReader reader = new CSVReader(new FileReader(csvFile), ';');
        String [] nextLine;
        int i = -1;
        String fileName = "";
        String companyName = "";
        String currency = "";
        String writerPath;
        List<String> returnList = null;
        List<String> dateList = null;

        while ((nextLine = reader.readNext()) != null && i < 10) {
            String[] line = nextLine;
            System.out.println(line[0]);
            System.out.println(line);
            i++;

            //fileName of the String 
            if(!line[0].contains("NULL")) {
                fileName = line[0];
            }

            writerPath = "C:\\Users\\Desktop\\CSVOutput\\" + fileName + ".csv";
            //write csv file
            CSVWriter writer = new CSVWriter(new FileWriter(writerPath), ';');

            //write Header
            String[] entries = "Name;Date;TotalReturn;Currency".split(";");
            writer.writeNext(entries);

            //create Content

            //companyName of the String 
            if(!line[1].contains("Name")) {
                companyName = line[1];
                System.out.println(companyName);
            }

            //currency 
            if(!line[2].contains("CURRENCY")) {
                currency = line[2];
            }


            //total returns
            returnList = new ArrayList<String>();
            if(line[0].contains("NULL")) {
                for(int j = 3; j <= line.length; j++) {
                    returnList.add(line[j]); // EXCPETION COMES HERE!
                }
            }

            //"Name;Date;TotalReturn;Currency"
            List<String[]> data = new ArrayList<String[]>();
            for(int m = 0; m <= line.length; m++) {
                data.add(new String[] {companyName, "lolo", "hereComesTheDateLater", currency});
            }

            writer.writeAll(data);

            //close Writer
            writer.close();
        }
        System.out.println("Done");


    }

}

我得到一个

java.lang.ArrayIndexOutOfBoundsException: 3039
    at com.TransformCSV.main.ParseCSV.run(ParseCSV.java:78)
    at com.TransformCSV.main.ParseCSV.main(ParseCSV.java:20)

在这一行:returnList.add(line[j]);?

为什么?有什么可能的方法来解决这个问题?

非常感谢您的回答!

最佳答案

你想要j < line.length并且不是 <= .如果有10数组中的元素,则索引 10 处没有项目- 你只有0-9 .

进一步使用大量变量并分配它们并不是解析 CSV 的首选方法。 Java 是一种面向对象的语言。

使用Object表示每一行并使用 opencsv javabean API 绑定(bind)行

关于java - ArrayIndexOutOfBoundsException - 解析 csv 文件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20813710/

相关文章:

java - 如何检测图像的黄色程度

JAVA解析txt文件中的数据

java - 从 csv 文件读取时忽略逗号值

java - 编辑 CSV 文件(设计实现)

用于下载 csv 文件的 Javascript 在 FireFox 中不起作用

python - 在 CSV 文件中查找特定的分割字符串 [Python]

java - NACHOS(JAVA版)教程【设置及简单示例】

java - 记得在所有 Realm 实例上调用 close() 即使我调用了 close()

java - 在java中读取不同行长度的CSV文件

java - 从java中的对象生成CSV文件