2

I am reading a csv file in java using CSVReader. It is "working" but I've found a minor problem when I try to split the line into an array of strings like this:

reader = new CSVReader(new FileReader(csvFile));
line = reader.readNext()
String[] lineDetail = line[0].split(";", -1);

Here is my problem: The line below work correctly:

[ABEL MESQUITA JR.;178957;1;2015;RR;DEM;55;1;MANUTENÇÃO DE ESCRITÓRIO DE APOIO À ATIVIDADE PARLAMENTAR;0;;WM PAPELARIA E ESCRITÓRIO;12132854000100;3592;0;2017-04-26 00:00:00;296;0;296;4;2017;0;;;1377952;5828;0;3074;6266962]

But the line below when I try to read using CSVReader, results in 3 arrays of strings:

[ABEL MESQUITA JR.;178957;1;2015;RR;DEM;55;3;COMBUSTÍVEIS E LUBRIFICANTES.;1;Veículos Automotores;B.B. PETROLEO LTDA;03625917000170;4339;0;2017-01-31 00:00:00;4007, 06;0;4007, 06;1;2017;0;;;1354058;5711;0;3074;6196889]

The arrays look like this:

ABEL MESQUITA JR.;178957;1;2015;RR;DEM;55;3;COMBUSTÍVEIS E LUBRIFICANTES.;1;Veículos Automotores;B.B. PETROLEO LTDA;03625917000170;4339;0;2017-01-31 00:00:00;4007

06;0;4007

06;1;2017;0;;;1354058;5711;0;3074;6196889

I think the problem is because of this value: 4007, 06 since in the first line the value is an integer 296.

Does anyone know how to make the CSVReader returns only one array, instead of 3?

Thanks in advance!!

EDIT 1

The result that I need is the second and third array concatenated with the first. So I would have the 4007,06 together instead of separated.

4
  • 1
    Where are you using CSVReader? It looks like you're just using String.split. Commented Sep 24, 2017 at 21:28
  • I will edit and add the code. Commented Sep 24, 2017 at 21:28
  • @AndyTurner I have edited. But the problem to be honest I think it is with the split call, CSVReader is returning the line correctly I guess Commented Sep 24, 2017 at 21:31
  • @AndyTurner Actually I was looking and the problem is definitely with the CSVReader because these 3 arrays that I have mentioned they are the line value that i get when call the readNext() Commented Sep 24, 2017 at 21:36

1 Answer 1

1

Your line of CSV data is getting split on comma, which is default CSV field separator.

To avoid splitting on commas, before reading your CSV file initialize your CSV reader to use some unused character (for example Tab) as CSV field separator. This way you will get one field per row. But your expectation seems to be pointless, because normally there should be easier way:

Why you don't configure CSV field separator to ; so it will split your CSV directly into semicolon-separated fields and you won't need to do additional splitting? This will also solve your problem with commas.

Your line[0].split(";", -1) is basically a bug, because it won't be able to split this valid CSV into 2 values:

Value 1; "Value;2"

Using your approach you will get 3 values instead.

To get further advice on CSVReader, please add the information which one are you using (by denoting its package).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.