3

I want to transform a csv file. My file looks like that:

enter image description here

I am using the opencsv libary to parse my csv. That is my run method to parse the file:

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");


    }

}

I am getting an

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

at this line: returnList.add(line[j]);?

Why? What are possible ways to fix that?

I really appreciate your answer!

1
  • 1
    You should use the debugger to help you debug this... Commented Dec 28, 2013 at 10:57

2 Answers 2

1

You want j < line.length and not <=. If there are 10 elements in an Array then there is not an item at index 10 - you only have 0-9.

Further using loads of variables and assigning them is not the preferred way to parse CSV. Java is an Object Orientated language.

Use an Object to represent each line and bind the line using the opencsv javabean API

Sign up to request clarification or add additional context in comments.

Comments

1

You are parsing the file till length of file <= instead you have to use <. It will access the file till line.length - 1

Replace with this

for(int j = 3; j <line.length; j++) {
     returnList.add(line[j]); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.