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 
6
  • 1
    change the separator of your columns Commented May 20, 2021 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. Commented May 20, 2021 at 13:54
  • Does this answer your question? How to handle commas in the data of a csv in Java Commented May 20, 2021 at 13:56
  • 2
    Probably the program that creates the CSV is broken and doesn't quote properly. The real solution is to fix that program, not mangle the input before passing it to the csv parser. Commented May 20, 2021 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. Commented May 20, 2021 at 14:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.