0
Exception in thread "main" org.supercsv.exception.SuperCsvException: The number of columns to be processed (229326) must match the number of CellProcessors (8):

I believe i may have to redo what im doing using supercsv as it may be easier in the long run however im open to any other suggestions. I simply want to write back to a csv file, i have a list with all the data in it however the ouput is like this

4350 02/06/2013 3965.21 0.0 0.0 0.0 0.0 0.0,
4698 02/06/2013 498.16 0.0 0.0 0.0 0.0 0.0, 
4992 02/06/2013 97.87 87.82 6.05 0.0 0.0 0.0,  
4441 02/06/2013 18.8 71.98 11.6 0.0 0.0 -42.5,  54092 02/06/2013 105.11 118.82 6.24 0.0 0.0 0.0,

I've managed to get the out put i want by replacing strings within the list however when it runs it hangs and i believe its due to how i'm writing back to the csv, i'm not sure, what else to do other than to write it back to the csv diffrently not using super csv. The error i get is

"1805","31/07/2013","-233.4","0.0","0.0","0.0","0.0","0.0"
"8054","31/07/2013","280.45","82.38","52.38","0.0","0.0","-200.0"The number of columns to be processed (1) must match the number of CellProcessors (8):

My witer class is as follows

package writer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.supercsv.cellprocessor.FmtDate;
import org.supercsv.cellprocessor.ParseDouble;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListWriter;
import org.supercsv.io.ICsvListWriter;
import org.supercsv.prefs.CsvPreference;


public class RentStatementsWriter {
public ArrayList rData;
private List<String> csvData;

char b = ',';

public RentStatementsWriter(ArrayList rentData)  {
    rData = rentData;
    ICsvListWriter listWriter = null;


    try{
        listWriter = new CsvListWriter(new FileWriter("rentl.csv"),CsvPreference.STANDARD_PREFERENCE);
        CellProcessor[] processors =new CellProcessor[]{
                new ParseInt(),
                new FmtDate("dd/MM/yyyy"),//
                new ParseDouble(),
                new ParseDouble(),
                new ParseDouble(),
                new ParseDouble(),
                new ParseDouble(),
                new ParseDouble(),

        };
        final String [] header = new String []{"_num", "End_Date", "bal_val","rval","cval","bf_val","aval","pval"};

        System.out.print("to string "+rData.toString().replaceFirst(" ", "\"").replaceAll("\\,"," \\,").replaceAll("  ", "").replaceAll(" ", "\"\\,\"").replaceAll("\"\\,\"\\,", "\"\n\""));
        csvData = Arrays.asList(rData.toString().replaceFirst(" ", "\"").replaceAll("\\,"," \\,").replaceAll("  ", "").replaceAll(" ", "\"\\,\"").replaceAll("\"\\,\"\\,", "\"\""));
        /*
         * replace
         * .replaceAll(" ", "\"\\,\"")
         */
        listWriter.writeHeader(header);
        listWriter.write(csvData, processors);



    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print(e+" file unable to write");
    } finally {
        if(listWriter !=null){
            try {
                listWriter.close();
            } catch (IOException e) {

                e.printStackTrace();
                System.out.println("list writer");
            }
        }

    }


}
String listToCsv(List<String> listOfStrings, char separator) {
    StringBuilder sb = new StringBuilder();

    // all but last
    for(int i = 0; i < listOfStrings.size() - 1 ; i++) {
        sb.append("\""+listOfStrings.get(i)+"\"");
        sb.append(separator);
    }

    // last string, no separator
    sb.append(listOfStrings.get(listOfStrings.size()-1));

    return sb.toString();
}





}

What am i missing in this syntax, or is there a better way of doing this task

j bel
  • 153
  • 6
  • 18
  • @SJuan76, you are wrong. This has nothing to do with Arrays. It's the SuperCSV Specific exception. – RaceBase Aug 06 '13 at 08:51
  • @Reddy when writing it to string it starts off with [" ..." ], i though that this is the format for a list period, would this be the reason its only readying the whole thing as one coll – j bel Aug 06 '13 at 09:11

3 Answers3

1

There's a couple of problems:

  1. ParseInt and ParseDouble are used for reading CSV Strings into Integer and Double respectively. See the handy table here to see what cell processors can be used for reading/writing or both. You can leave them null if you want, and Super CSV will just call toString() on each object.

  2. The exception you're getting (1 column / 8 processors) indicates that you're expecting there to be 8 columns (i.e. 8 elements in your List), but there's only 1. You're only passing a single String into Arrays.asList() - looks like you're assuming this method actually splits the String into a List (it doesn't!).

  3. Why are you converting your rent data List to a String? That is really bizarre. If your data needs any manipulation (I'm not sure if it does), then you should be updating each element in your List, not converting the whole List to a String then trying to split it up again.

  4. What is the output when you don't do any replacement, i.e. what happens when you pass your rentData list directly to listWriter.write()?

Can I suggest you fix the processor set up (replace the ParseInt and ParseDouble with null), pass the rentData List directly to Super CSV...

listWriter.write(rentData, processors);

...then post the result (output/stacktrace) to your question.

James Bassett
  • 9,458
  • 4
  • 35
  • 68
  • rentdata list doesnt have any commas or speech marks, just the numbers with a space hence all the replaces..Your exactly right, however i cant think of another way of doing it @Hound Dog – j bel Aug 07 '13 at 22:32
  • @jbel Just checking, but you _are_ aware that Super CSV puts the quotes and commas in for you? You should be just passing your List of Strings to Super CSV and letting it do all the work. – James Bassett Aug 08 '13 at 02:31
  • i tried putting the list straight in but i get error the number of coll to be processed 229326 must match cellprocessor (8) which gave me the hint that i must put the "" and , in – j bel Aug 11 '13 at 12:25
  • Sorry I should have noticed this before, but you can't just write a single List - each call to `write()` writes 1 line of CSV from 1 List (see how the [example](http://supercsv.sourceforge.net/examples_writing.html#Writing_with_CsvListWriter) on the website calls `write()` twice to write 2 lines?). Each List should only have 8 elements in it. So you will have to pass multiple Lists into your method (a List of Lists), or create a POJO and pass in a List of them (and use `CsvBeanWriter` instead). – James Bassett Aug 11 '13 at 21:51
  • That makes more sense, @Hound Dog im able to do this `listWriter.write(rentData.get(i), processors);` but it outputs a load of nonsense at the end [lorg.supercsv] etc. any way of getting rid of that ? – j bel Aug 11 '13 at 22:40
  • Sounds like you're accidentally using `write(Object... columns)` instead of `write(List columns, CellProcessor[] processors)`. That's why it's important to use generics in Java. Make sure the definition of your `rentData` List is `List> rentData` so that the compiler knows that `rentData.get(i)` returns a `List`. If you hover over `listWriter.write()` in your IDE, it should tell you which method you're using. – James Bassett Aug 11 '13 at 23:10
0

The Solution to this question lies in the question.

The number of columns to be processed (1) must match the number of CellProcessors (8):

You are actually having more number of comma separated data in a row than you initially told SuperCSV.

final String [] header = new String []{"_num", "End_Date", "bal_val","rval","cval","bf_val","aval","pval"};

This puts SuperCSV to assume that only 8 number of values expected for each row and each column corresponds to one header. If you pass more values in row, it doesn't know what's that value corresponds to, so it's throwing Exception.

This link gives you how to define optional/mandatory columns.

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • i figured that, but i thought that this is the right format for writing to csv @Reddy `"1805","31/07/2013","-233.4","0.0","0.0","0.0","0.0","0.0"` – j bel Aug 06 '13 at 09:07
  • i did all as new optional, and that hasn't helped, i still get the same error – j bel Aug 06 '13 at 13:27
0

@Hound Dog Although you was probably right i couldn't get it working the way i wanted it myself. I changed the list to of type string and was stil getting that[lorg supercsv] crap, i just decided to quit with the super csv thing, just in case anybody runs into this issue doing it like this i found to be be easier. The stuff in the constructor is not needed apart from the generate csv method

package writer;




public class RentStatementsWriter {
/*
 * 
 * problem being that for super csv to work each entry will need a seperate list
 */
public ArrayList<RentStatements> rData;
private List<String> csvData;

char b = ',';

public RentStatementsWriter(ArrayList rentData)  {
    rData = rentData;

    csvData = new ArrayList<String>();



    try{


        generateCsvFile("renttest.csv", rentData,b);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print(e+" file unable to write");
    } finally {
        if(listWriter !=null){
            try {
                listWriter.close();
            } catch (IOException e) {

                e.printStackTrace();
                System.out.println("list writer");
            }
        }

    }


}

private void generateCsvFile(String fileName, ArrayList rentData, char b2) {

    try{
        final String [] header = new String []{"tncy_num", "End_Date", "bal_val","rent_val","chg_val","benf_val","adj_val","pay_val"};
        FileWriter writer = new FileWriter(fileName);


        writer.append("tncy_num");
        writer.append(',');
        writer.append("End_Date");
        writer.append(',');
        writer.append("bal_val");
        writer.append(',');
        writer.append("rent_val");
        writer.append(',');
        writer.append("chg_val");
        writer.append(',');
        writer.append("benf_val");
        writer.append(',');
        writer.append("adj_val");
        writer.append(',');
        writer.append("pay_val");
        writer.append('\n');
        for(int i = 0;i <rentData.size();i++){
            String line = rentData.get(i).toString();

            String  bits []=line.split(" ");//splits each space into diffrent bits

            //string something = bits.get waleva the it is surround it by ""
             writer.append(bits[1]);

                writer.append(b2);

                writer.append(bits[2]);

                writer.append(b2);

                writer.append(bits[3]);

                writer.append(b2);

                writer.append(bits[4]);

                writer.append(b2);

                writer.append(bits[5]);

                writer.append(b2);

                writer.append(bits[6]);

                writer.append(b2);

                writer.append(bits[7]);

                writer.append(b2);

                writer.append(bits[8]);

                writer.append('\n');


        }


        writer.flush();
        writer.close();

    }catch(IOException e)
    {

        e.printStackTrace();
    }
    // TODO Auto-generated method stub

}

}

j bel
  • 153
  • 6
  • 18