-3

I am reading a csv file and then writing it to other file with additional column, when I am moving the data from a list to the additional column I got IndexOutOfBounds

private void writeDataToCSVDestinationFile(Path csvPath, Path destPath, String colToNormalize) throws IOException, CsvException {
        CSVWriter writer = new CSVWriter(new FileWriter(destPath.toString()));
        CSVReader reader = new CSVReader(new FileReader(csvPath.toString()));
        
        String [] headerDes = new String [header.length+1];  
        for (int i=0; i<header.length; i++){
        headerDes[i]= header[i];
        }
        headerDes[header.length]= colToNormalize+"_scaled";
        writer.writeNext(headerDes);
        
       String[] eachLineInTheOriginalFile;
       String[] lineToBeWritten = new String[headerDes.length];
         int j =0;
       while ((eachLineInTheOriginalFile=reader.readNext())!=null){
             
            for (int i =0; i<eachLineInTheOriginalFile.length; i++ ){
            lineToBeWritten[i] = eachLineInTheOriginalFile[i];
            }
            lineToBeWritten[eachLineInTheOriginalFile.length]=dataAfterNormalization.get(j).toString(); //the line  generate the exception
            j++;
        }
       writer.writeNext(lineToBeWritten);
    } 

Any one can help me to solve it.

  • 1
    You set as `String[] lineToBeWritten = new String[headerDes.length];` why would you expect `lineToBeWritten[eachLineInTheOriginalFile.length]` to be relevant? – Scary Wombat Jul 08 '21 at 01:24

3 Answers3

0

There is no index j in dataAfterNormalization, so your get is throwing that error.

You also have another problem on that line. The final index of lineToBeWritten is eachLineInTheOriginalFile.length - 1

  • What do you mean no index `j`. It starts at `0` and continues incrementing. – WJS Jul 08 '21 at 01:40
  • Correct, `j` contains a value. At some point in the loop, the dataAfterNormalization object does not have anything at index `j`. When get(j) is called on it it throws an exception. – James Schultz Jul 08 '21 at 01:50
  • @ James Schultz How can I solve it please – shahed aa Jul 08 '21 at 01:54
  • I can't tell from your snippet, is start by looking at where dataAfterNormalization comes from. Check `dataAfterNormalization.size()` and make sure it is >= `eachLineInTheOriginalFile.length`. – James Schultz Jul 08 '21 at 02:00
0

The reason the reported error might be, the number of elements in the list "dataAfterNormalization" are less than the number of rows(lines of data) in the soure csv (eachLineInTheOriginalFile)

Giri
  • 14
  • 1
-1
This code may work for you 

package index;

import  java.util.List;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.exceptions.CsvException;

public class Sample {

    public static void main(String[] args) {
        List<String> dataAfterNormalization=new ArrayList<String>();  
        dataAfterNormalization.add("Development");
        dataAfterNormalization.add("QA");
        
        try{
        Path csvPath = Paths.get("C:\\YourFolderName\\source.csv");
        Path destPath = Paths.get("C:\\YourFolderName\\destination.csv");
        writeDataToCSVDestinationFile(csvPath,destPath,"Department", dataAfterNormalization);
        }catch (Exception e) {
            e.printStackTrace();
        }
                    
    }
    
private static void writeDataToCSVDestinationFile(Path csvPath, Path destPath, String colToNormalize, List<String> dataAfterNormalization) throws IOException, CsvException {
    CSVWriter writer = new CSVWriter(new FileWriter(destPath.toString()));
    CSVReader reader = new CSVReader(new FileReader(csvPath.toString()));
    
    String[] header = reader.readNext(); // assuming first read
    String [] headerDes = new String [header.length+1];  
    for (int i=0; i<header.length; i++){
    headerDes[i]= header[i];

    }
    headerDes[header.length]= colToNormalize+"_scaled";
    writer.writeNext(headerDes);

   String[] eachLineInTheOriginalFile;
   String[] lineToBeWritten = new String[headerDes.length];
     int j =0;
   while ((eachLineInTheOriginalFile=reader.readNext())!=null){
         
        for (int i =0; i<eachLineInTheOriginalFile.length; i++ ){
        lineToBeWritten[i] = eachLineInTheOriginalFile[i];
        }
        lineToBeWritten[eachLineInTheOriginalFile.length]=dataAfterNormalization.get(j).toString();
        j++;
        writer.writeNext(lineToBeWritten);
    }
   writer.close();
   reader.close();
} 

}
Giri
  • 14
  • 1