0

I was trying to write some formula data in a csv file using the openCSV java library, but it only wrote about 90% of the string in each cell. Are there any known issues that might cause this behavior?

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

        FileOutputStream fosW = new FileOutputStream("C:\\Users\\Desktop\\formula.csv");
        OutputStreamWriter osw = new OutputStreamWriter(fosW, "UTF-8");
        CSVWriter writerCSW = new CSVWriter(osw);

        int k = 0;
        String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT"};
        String[] line = new String[100];
        for (int i = 0; i < b.length - 1; i++) {
            String a = "=IF(legend!" + b[i] + "$3='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$3,IF(legend!" + b[i] + "$4='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$4,IF(legend!" + b[i] + "$5='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$5,IF(legend!" + b[i] + "$6='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$6,IF(legend!" + b[i] + "$7='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$7,IF(legend!" + b[i] + "$8='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1] + "$8,#error#))))))";
            System.out.println(a);
            line[k] = a;
            k++;
        }
        writerCSW.writeNext(line);

    }
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Marc Zaharescu
  • 629
  • 1
  • 13
  • 34
  • Hi. I think you assume that each index in line[] is 1 line in your csv. In fact, the way you use it, line[] is eactly 1 entry in your CSV file. Look at the API docs, writeNext takes an array that represents 1 line in the CSV file. So essentially, you need to restructure your code to write each line within your for loop. – pandaadb Jun 09 '16 at 09:40
  • @pandaadb I know that that it would write everything in 1 line, that was the intention – Marc Zaharescu Jun 09 '16 at 10:03

1 Answers1

2

I believe your issue is that you are using the library wrong. (though this might be me misunderstanding.

Your B has a length of 46. Your CSV file will have 45 entries.

The line[k] is exactly 1 line. I think you are assuming that each index in the line array is. This is mistake number 1 (maybe).

But the reason your file is not fully populated is because you are not closing the stream, so data gets lost once your program exist. So you'll want to close your writer, or better yet, use a try-with-resources block. I have rewritten your code to do this:

public static void main(String[] args) throws IOException {

        FileOutputStream fosW = new FileOutputStream("/home/artur/tmp/test.csv");
        OutputStreamWriter osw = new OutputStreamWriter(fosW, "UTF-8");
        try (CSVWriter writerCSW = new CSVWriter(osw)) {

            String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                    "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ",
                    "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT" };

            System.out.println(b.length);
            for (int i = 0; i < b.length - 1; i++) {
                String a = "=IF(legend!" + b[i] + "$3='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
                        + "$3,IF(legend!" + b[i] + "$4='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
                        + "$4,IF(legend!" + b[i] + "$5='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
                        + "$5,IF(legend!" + b[i] + "$6='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
                        + "$6,IF(legend!" + b[i] + "$7='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
                        + "$7,IF(legend!" + b[i] + "$8='Form responses 1'!" + b[i + 1] + "2,legend!" + b[i + 1]
                        + "$8,#error#))))))";
                String[] line = a.split(",");
                writerCSW.writeNext(line);
                System.out.println(i);
            }
        }

    }

Note, I have changed a bit of things so it might not be doing exactly what you want. However, you should be able to see what went wrong from this example.

Please Note: See how I split your String a by commas? This is to generate an array for the CSV input. It will automatically insert your chosen delimiter for you. If you do not want to do this, get rid of the CSVWriter, and use a normal Filewriter. You generate your CSV line manually by constructing the String. This is not necessary when using OpenCSV.

I hope that helps,

Artur

pandaadb
  • 6,306
  • 2
  • 22
  • 41
  • thanks for your answer as I stated on your comment above I understand that the output would be on 1 line that was my intention, since I am only in generating a computing formula in excel. Anyway you were right that I forgot to close the writer, that solved my issue. Tnx – Marc Zaharescu Jun 09 '16 at 10:08
  • Ah great :) I just wanted to point out the line thing because I found your example confusion. If that was your intention, then that's perfectly fine. You can accept the answer then if it solved your issue :) – pandaadb Jun 09 '16 at 10:18