Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A developer parses CSV with line.split(",") and imports go wrong for some rows. Give three kinds of input that break it and what you would use instead.

line.split(",") treats every comma and every line break as a boundary, but CSV allows both inside quoted fields. Three inputs that break it are a quoted field with a comma such as "Smith, John", a quoted field with a line break, and rows with escaped quotes or empty trailing fields, which split drops; a byte order mark at the start of the file is a fourth. We should use a CSV library that follows RFC 4180, such as Apache Commons CSV, OpenCSV or Jackson's CSV module, and read the file with an explicit charset.

Why this question is asked

CSV looks like the simplest format there is, which is exactly why so many importers are written with split and work until the first real customer file. Then rows shift by one column, amounts land in the name field, or a single address with a line break turns one row into two broken ones. Interviewers ask this because it shows whether we know that CSV has quoting rules, and whether we reach for a proper parser instead of patching regular expressions. In this answer, we will recap what a CSV file actually allows, break split with each kind of input, see why reading line by line cannot work, look at encoding problems, and replace the code with a library.

1A Quick Recap: What CSV Actually Allows

CSV is described by RFC 4180 and by the habits of tools like Excel. Fields are separated by commas and rows by line breaks. But a field may be wrapped in double quotes, and inside quotes the rules change: a comma is part of the value, a line break is part of the value, and a double quote is written as two double quotes. So the row 1,"Smith, John","He said ""hi""",9 has exactly four fields: 1, then Smith, John, then He said "hi", then 9.

split(",") knows none of this. It cuts at every comma, keeps the quote characters, and only sees one physical line at a time.

2Breaking split With Each Kind of Input

Here are the problems one by one, with what split actually returns.

Java
"1,\"Smith, John\",42".split(",");
// ["1", "\"Smith", " John\"", "42"] -> 4 fields instead of 3, quotes left in
"1,\"He said \"\"hi\"\"\",42".split(",");
// ["1", "\"He said \"\"hi\"\"\"", "42"] -> quotes not unescaped
"7,Berlin,,".split(",");
// ["7", "Berlin"] -> trailing empty fields dropped: 2 instead of 4
"7,Berlin,,".split(",", -1);
// ["7", "Berlin", "", ""] -> limit -1 keeps them
// A quoted address with a line break, read with readLine():
// line 1: 3,"221B Baker Street
// line 2: London",GB
// -> two broken rows instead of one
Inputs that break split(",")
InputWhat split doesCorrect result
1,"Smith, John",424 fields, quotes kept3 fields: 1 | Smith, John | 42
1,"He said ""hi""",42doubled quotes keptHe said "hi"
7,Berlin,,2 fields4 fields, last two empty
quoted field with a line breakrow split across two linesone row
BOM + id,name headerfirst header is "\uFEFFid"header id

Notice the third case. String.split removes trailing empty strings unless we pass a negative limit, so a row whose last two columns are empty suddenly has two fields, and code that reads fields[3] throws ArrayIndexOutOfBoundsException, or worse, a shorter row is silently accepted. Only some rows fail, the ones with empty optional columns at the end, which is why the bug looks random.

3Why Reading Line by Line Cannot Work

The line break case is the one that cannot be patched with a smarter split. BufferedReader.readLine stops at every line break, including one inside quotes, so our code receives half a row. To know whether a line break ends the row, the parser must know whether it is currently inside a quoted field, which depends on everything before it. That is a small state machine, reading character by character across lines, and it is exactly what a CSV library implements. Every attempt to fix this with regular expressions ends up as a fragile, partial version of that state machine.

4Encoding and the Byte Order Mark

The last class of problems is about bytes, not commas. Excel's plain CSV export on Windows often writes Windows-1252, where characters like e-acute are single bytes that are invalid in UTF-8, while its "CSV UTF-8" option writes UTF-8 with a byte order mark, the bytes EF BB BF, at the start of the file. Read with the platform default or the wrong charset, names come out garbled. And if the BOM is not removed, the first header is not id but an invisible character followed by id, so a lookup by header name fails for the first column only.

Java
// Reading with an explicit charset, and dropping a BOM if present
BufferedReader r = Files.newBufferedReader(path, StandardCharsets.UTF_8);
r.mark(1);
if (r.read() != '\uFEFF') r.reset(); // not a BOM: go back to the start

So the charset must be part of the import's contract: agreed with the partner, configured per feed, and stated explicitly in code rather than left to the default.

5Replacing split With a CSV Library

Apache Commons CSV, OpenCSV and Jackson's CSV module all implement the quoting rules, handle line breaks inside quotes, keep empty fields, and let us configure the delimiter, quote character and header handling. With Commons CSV it looks like this.

Java
CSVFormat format = CSVFormat.RFC4180.builder()
.setHeader() // read the header from the first record
.setSkipHeaderRecord(true)
.setIgnoreSurroundingSpaces(true)
.build(); // newer versions also offer get(); check your version
try (Reader in = Files.newBufferedReader(path, StandardCharsets.UTF_8);
CSVParser parser = format.parse(in)) {
for (CSVRecord row : parser) {
String name = row.get("name"); // "Smith, John"
long line = parser.getCurrentLineNumber(); // for error messages
import(row.get("id"), name, row.get("amount"));
}
}

Notice that we read fields by header name instead of by position, so a partner adding a column does not shift everything, and we can report the line number of a bad row. The parser streams records one at a time, so it works for large files too. For semicolon-separated exports, common where the comma is the decimal separator, we change the delimiter in the format instead of rewriting code.

6Follow-up questions

Is it ever fine to use split for CSV?

Only when we control the producer and the format is guaranteed never to contain quotes, commas or line breaks inside values, such as numeric data written by our own job. Even then, split(",", -1) should be used so empty trailing fields are kept.

How would you test the importer against these cases?

Keep a small set of fixture files with each tricky case, a comma in quotes, a line break in quotes, doubled quotes, empty trailing fields, a BOM and a Windows-1252 file, and assert the parsed values. Those files catch regressions when someone changes the parser configuration.

What about very large CSV files?

The library approach scales, because parsers read record by record from a Reader. The rest of the import, batching inserts and reporting bad rows, is where the design work goes.

7Conclusion

In this answer, we saw that CSV allows commas, line breaks and doubled quotes inside quoted fields, none of which split(",") understands, and that split also drops trailing empty fields unless the limit is -1. Reading line by line breaks quoted line breaks apart, and a wrong charset or an unremoved BOM corrupts names and the first header. We replaced it with an RFC 4180 parser such as Commons CSV, configured the format and headers, and read with an explicit charset.

What to say in the interviewAbout 90 seconds aloud

Open with

split on a comma treats CSV as if it had no quoting rules, but it does. Three inputs break it. First, a quoted field with a comma, like "Smith, John": split cuts it into two fields and keeps the quote characters, so every column after it shifts by one.

Then, if they want more

Second, a quoted field with a line break, like an address over two lines: since the code reads line by line, one record becomes two broken ones, and you cannot fix that with a better split because you need to know whether you are inside quotes.

Third, escaped quotes, written as two double quotes inside a quoted field, are not unescaped, and rows with empty trailing fields lose them, because String.split drops trailing empty strings unless you pass a limit of minus one, which is why only some rows fail.

I would also check encoding: Excel may export Windows-1252, or UTF-8 with a byte order mark that turns the first header into an invisible character plus id.

The fix is a real CSV parser that follows RFC 4180, Apache Commons CSV, OpenCSV or Jackson's CSV module, configured with the delimiter, quote and header handling, reading fields by header name, and a reader with an explicit charset. And I would add fixture files for each of those cases to the tests.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A developer parses CSV with line.split(",") and imports go wrong for some rows. Give...": https://scrivio-interview-library.netlify.app/java/io-and-nio/a016540c92/