'Stop comma in csv row from splitting the column in two Java

Here is my code

     BufferedReader br = new BufferedReader(new InputStreamReader(sr));
      String splitBy = ",";
      String line = br.readLine();
      while((line = br.readLine()) != null){
        String[] b = line.split(splitBy);
        System.out.println("\"" + b[0] + "\",\"" +b[4] + "\",\""+ b[6] + "\"");
      }
      br.close();
    }
  }

The columns in my csv file should print out like this

"John", "Smith", "Smith,John"  

but it takes the comma in the column and splits it into two columns like this;

""Smith" John"", "John", "Smith"

How can I get it to ignore the column that is in the column and not split it into two columns AND stop it from adding double quotes.

Thanks in advance



Solution 1:[1]

If you have commas in your data rows then change your separator. Use ;. There is no way for the program to know when to skip the delimiter and when not...

CSV can have any separator that you find suitable (some use :, @, ;, |, etc..)

Solution 2:[2]

Do not split by COMMA first. You must first split by the pairs of QUOTATION MARK enclosing each field. See the CSV specification.

I recommend you make use of an existing CSV parsing library rather than write your own. You have a choice of several good libraries in the Java ecosystem. For example, I have used Apache Commons CSV in a few projects. More libraries are mentioned in this Comment.

Solution 3:[3]

Modify your regex to split on all comma characters unless it's in between quotes.

String[] b = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

See the acepted answers in these posts:

Java: splitting a comma-separated string but ignoring commas in quotes

Splitting on comma outside quotes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Renis1235
Solution 2 Basil Bourque
Solution 3 Eritrean