12

I have the below block of code which uses OpenCSV to read a CSV file and store the 7th column. The problem I face is that I use ; as delimiter in the CSV file but it takes , as delimiter as well. How can I avoid this?

Putting "" in CSV is not possible, since we get a non-editable file from client.

        CSVReader reader = null;
    String[] nextCsvLine = new String[50];
    String splitBy = ";";

    int count = 0;

    try {
        StringReader sr = new StringReader(new String(in, offset, len));
        reader = new CSVReader(sr);

        while ((nextCsvLine = reader.readNext()) != null) {
            for (String linewithsemicolon : nextCsvLine) {
                log.debug("Line read : "+linewithsemicolon);
                String[] b = linewithsemicolon.split(splitBy);
                if (count==0){
                    count++;
                    continue;
                }
                else    {      
                    detailItems.add(b[7]);
                    log.debug("7th position: "+b[7]);
                    count++;
                }                   
            }
Sourav Mehra
  • 435
  • 2
  • 7
  • 23
  • Which language is this? It looks like Java. You need to tag your question with the programming language - see the "edit" link under your question. – Erwin Bolwidt Oct 20 '17 at 09:06
  • Yes its JAVA sorry forgot to link. – Sourav Mehra Oct 20 '17 at 09:11
  • 1
    The code you posted doesn't react on commas at all. Please provide a [mcve]. – Thomas Oct 20 '17 at 09:12
  • To simplify my question, is it possible that I only read till this 8th column and then skip to the next line?. Line may have 30 columns or even more but I just want to read 8th column and move to next line. – Sourav Mehra Oct 20 '17 at 09:13
  • https://stackoverflow.com/questions/5993779/use-string-split-with-multiple-delimiters this will help you -> same approach – Daniel Stiefel Oct 20 '17 at 09:14
  • http://opencsv.sourceforge.net/apidocs/com/opencsv/CSVParserBuilder.html#withSeparator(char). – JB Nizet Oct 20 '17 at 09:15
  • 1
    Note that `for (String linewithsemicolon : nextCsvLine)` already indicates your problem: `nextCsvLine` indicates a line represented as a collection or array of elements while `linewithsemicolon` indicates you're treating _one_ element as a line. That doesn't fit well and should already hint at problems with the reader. As already answered you can provide a delimiter to the CSVReader but in case this were not possible then using CSVReader at all would have been a questionable decision. – Thomas Oct 20 '17 at 09:17
  • My point is even if I don't want this replacement thing at all, is it possible to just skip the entire line after reading the 8th column. – Sourav Mehra Oct 20 '17 at 09:23
  • Thomas can you help with this? – Sourav Mehra Oct 20 '17 at 10:54

1 Answers1

22

Use the overloaded version with separator of OpenCSV

CSVReader(reader, ';')

Update (thanks to @Matt) - better use:

CSVReaderBuilder(reader)
    .withCSVParser(new CSVParserBuilder()
        .withSeparator(';')
        .build()
    ).build()

I think the counting was done a bit wrong:

try (CSVReader reader = new CSVReader(sr, ';')) {
    String[] nextCsvLine;
    while ((nextCsvLine = reader.readNext()) != null) {
        int count = 0;
        for (String field: nextCsvLine) {
            log.debug("Line read : "+linewithsemicolon);
            if (count == 6) { // 7th column
                detailItems.add(field);
                log.debug("7th position: " + field);
            }                   
            count++;
        }
    }

Instead the for-loop you could have done:

         if (nextCsvLine.length > 6) {
             detailItems.add(nextCsvLine[6]);
         }

Where the seventh field should have index 6.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Did you mean something like reader = new CSVReader(sr,';'); This breaks after the first instance of semicolon and doesn't read after that. – Sourav Mehra Oct 20 '17 at 10:48
  • See [ReadNumbers2.java](http://zetcode.com/articles/opencsv/) from zetcode.com. I'll add some code to the answer. – Joop Eggen Oct 20 '17 at 11:06
  • 5
    Fyi: As of the latest version OpenCSV uses the builder pattern which renders the constructor with the delimiter as 2nd parameter deprecated. It's now `CSVReaderBuilder(reader).withCSVParser(CSVParserBuilder().withSeparator(';').build())` See http://opencsv.sourceforge.net/apidocs/com/opencsv/CSVReader.html – Jan B. Aug 01 '18 at 15:11
  • 2
    @Matt I took the liberty to move the code into the answer to prevent deprecated code copies. – Joop Eggen Aug 01 '18 at 15:19