0

I am reading several csv sheet with the opencsv libary. I am storing the "read" csv file in hugeList = reader.readAll(); Now I only want to only write the 5th value of this huge csv sheet columnwise in another sheet.

At the moment I am standing at:

        //reading all entries in a huge list
        //-740 as it would take to much time to run it in "dev mode"
        for (int j = 0; j < (fileList.size() - 740); j++) { 
            String csvFile = "C:\\Users\\" + fileList.get(j);
            reader = new CSVReader(new FileReader(csvFile), ';');
            hugeList = reader.readAll();            

            List<String[]> data = new ArrayList<String[]>();
//          for(int m = 0; m < hugeList.size(); m++) {
//              String[] value = hugeList.get(m);
                data.add(hugeList.get(5));
//          }

            writer.writeAll(data);

        }

However I have no idea how to write them into another column and just take the 5th value?

I really appreciate your answer!

UPDATE 1

Goal: I have saved all values from one sheet at the time in hugeList. Now I want to write only the 5th column into a new sheet.

Problem: The logic behind this goal.

UPDATE 2

enter image description here

This is what my code looks like right now:

//reading all entries in a huge list
for (int j = 0; j < (fileList.size() - 740); j++) { 
    String csvFile = "C:\\" + fileList.get(j);
    reader = new CSVReader(new FileReader(csvFile), ';');
    hugeList = reader.readAll();            

    List<String[]> data = new ArrayList<String[]>();
    List<String> tmp= new ArrayList<String>();
     for(int m = 0; m < hugeList.size(); m++) {
              String[] values = hugeList.get(m);
              tmp.add(values[0]);       
       }
     data.add(tmp.toArray(new String[0]));


    writer.writeAll(data);

}

As you can see I get my data still vertically... Why?

user2051347
  • 1,609
  • 4
  • 23
  • 34
  • could you please elaborate the requirement and the problem – Anubhab Jan 07 '14 at 12:55
  • Try to explain your code in [SSCCE format](http://sscce.org/). – Not a bug Jan 07 '14 at 13:00
  • Why are you iterating over the inner array? Isn't `hugeList` just an array of `String` arrays or something? The inner array should be the csv lines, broken into fields, so you should be able to simply do `writer.write(hugeList[i][5])`. I think you are over-complicating things by trying to make a `List`. – CodeChimp Jan 07 '14 at 13:02
  • 1
    As I understand the question: The OP is getting the list of values from the csv sheet from `fileList` in `hugeList`. Then ,I think, he wants to give out only the 5th value of each sheet in a column. Is thi correct user2051347? – Carol.Kar Jan 07 '14 at 13:05
  • @user1000 yes, that is basically what I meant! – user2051347 Jan 07 '14 at 13:07
  • 1
    @user2051347 look at my update i thinks it can help you – Festus Tamakloe Jan 07 '14 at 14:40

1 Answers1

2

I thing you are already on the right way.

You have 2 possibilities.

1. data.add(hugeList.get(4));

2. for(int m = 0; m < hugeList.size(); m++) {
         if(m==4){
              String[] values = hugeList.get(m);
              data.add(values);
          break;
         }
    }

UPDATE
Goal: I have saved all values from one sheet at the time in hugeList. Now I want to write only the 5th column into a new sheet. Problem: The logic behind this goal.

enter image description here My Approach for a result in rows

List<String[]> data = new ArrayList<String[]>();
        List<String> tmp= new ArrayList<String>();
         for(int m = 0; m < hugeList.size(); m++) {
                  String[] values = hugeList.get(m);
                  tmp.add(values[4]);       
           }
         data.add(tmp.toArray(new String[0]));
        }

My Approach for a result in Column

 for (int j = 0; j < fileList.size(); j++) {
    String csvFile = readPath + fileList.get(j);
    System.out.println("Read:  " + csvFile);
    reader = new CSVReader(new FileReader(csvFile), ';');
    hugeList = reader.readAll();

    String[] data = new String[1];
    for (int m = 0; m < hugeList.size(); m++) {
        String[] values = hugeList.get(m);
        data[0] = values[0];
        writer.writeNext(data);
    }
}
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65