Question

I try and write my array to a file, however instead of my data, all I get is two cells; One is [Ljava.lang.String;@7b1ddcde and the other is [Lorg.supercsv.cellprocessor.ift.CellProcessor;@658fb1f7 I have no idea how on earth this is happening, as my array is actually a very nice set of data and works when I print it to the console.

My table has all of the headers and required cells, it's just that these weird errors take up the first two cells and no other data is written.

My CellProcessor method is just full of new Optional(), so I don't think that's the issue here. Here is the entire method. I am so sorry, my code is horrible it was made in a hurry.

static String[] writeWithCsvListWriter(String filename) throws Exception {
    ICsvListWriter listWriter = null;
    try {
            listWriter = new CsvListWriter(new FileWriter(Main.filename + ".csv"),
                    CsvPreference.STANDARD_PREFERENCE);

            final CellProcessor[] processors = getProcessors();
            final String[] header = new String[] { "Year", "mm", "Max Temp", "Min Temp",
                    "af days", "Rain", "Sun" };

            listWriter.writeHeader(header);

            FileReader fileReader = new FileReader(filename);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            List<String> lines = new ArrayList<String>();
            String line = null;

            int i = 0;
            while ((line = bufferedReader.readLine()) != null) {
                if(line.startsWith("   2010")){
                    try {
                        String[] splitArray = line.split("\\s+");
                        System.out.println("Writing Array");
                        listWriter.write(splitArray, processors);
                    } catch (PatternSyntaxException ex) {
                    }
                }
                i++;
            }
            bufferedReader.close();
    } finally {
            if( listWriter != null ) {
                    listWriter.close();
            }
    }
    return null;
}
Was it helpful?

Solution

Looking at the ICsvListWriter API, it looks to me like you are getting the method write(Object... columns), rather than write(List<?> columns, CellProcessor[] processors).

The method you are trying to call appears to be expecting a List as the first argument, rather than an array, so transform your array to a list first, then attempt to write it. Something like;

String[] splitArray = line.split("\\s+");
List list = Arrays.asList(splitArray);
System.out.println("Writing Array");
listWriter.write(list, processors);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top