0

Sample Data file

INV1,A,BB,,,,
INV2,A,CC,,,,
INV3,C,BB,,,,

array getting only INV1 A and BB for first line, similarly INV2 a and CC for second and INV3 C and BB for third line. Remaining values are not considering.

My Code

        BufferdReader bReader = new BufferedReader (new FileReader(/home/test.txt));
        String line = "";
        while ((line = bReader.readLine ()) !=null)
    {
 if (line != null)
{
 String[] array = line.split(",") ;
  for (String arrays : array ) {
                System.out.println(arrays );
            }
}
}

OUT PUT :

INV1
A
BB
INV2
A
CC
INV3
C
BB

Expected OUT PUT

INV1
A
BB



INV2
A
CC



INV3
C
BB
Viru
  • 525
  • 2
  • 7
  • 15

2 Answers2

2

I think it has to do with the split function of the String class. This line should do the trick.

"INV1,A,BB,,,,".split(",", -1);

This answer explains it well enough: String split.

Angel D.
  • 162
  • 2
  • 11
2

Add a limit to the split method:

String[] array = line.split(",", -1);

This is because the empty strings from the right are trimmed if you don't provide a limit. This behaviour is explained in this answer.

Kartik
  • 7,677
  • 4
  • 28
  • 50