1

I have the following code to read from CSV file in java. It works fine except for the case when I have commas in my column values. I got the exception.

code:

FileReader fileReader = new FileReader(file);
CSVReader csvReader = new CSVReader(fileReader);
String[] headers = csvReader.readNext();
List<String[]> rows = csvReader.readAll();

Exception:

Exception in thread "main" com.opencsv.exceptions.CsvMalformedLineException: Unterminated quoted field at end of CSV line. Beginning of lost text
AVMathi
  • 71
  • 8
  • 1
    change the separator of your columns – Stultuske May 20 '21 at 13:54
  • 2
    The `CSVReader` class *should* be able to handle this. It is probable that your CSV input is *not correct* and has formatting errors, which causes the error message. – markspace May 20 '21 at 13:54
  • Does this answer your question? [How to handle commas in the data of a csv in Java](https://stackoverflow.com/questions/15979688/how-to-handle-commas-in-the-data-of-a-csv-in-java) – azurefrog May 20 '21 at 13:56
  • 2
    Probably the program that creates the CSV is broken and doesn't [quote properly](https://datatracker.ietf.org/doc/html/rfc4180#section-2). The real solution is to fix that program, not mangle the input before passing it to the csv parser. – Robert May 20 '21 at 14:09
  • It seams to me, that the quotes of the line aren't correct. It should look like this: "field","field","field" If your fields aren't quoted, try it with the quotes. Commas inside will not harm any more. – Jordi Laforge May 20 '21 at 14:35
  • Please post the offending line so we do not have to guess. And edit your question which CSVReader you are using. – Queeg May 23 '21 at 06:23

0 Answers0