Check out the below program.
public class test {
/**
* @param args
*/
public static void main(String[] args) {
try {
String data = null;
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new FileReader(new File("D:/sample.txt")));
while((data=br.readLine())!= null){
String[] de = data.split(";");
System.out.println("Length ="+de.length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sample.txt:
1;2;3;4 A;B;; a;b;c;
Output:
Length =4 Length =2 Length =3
Why second and third output is giving 2 and 3 instead of 4. In sample.txt
file, condition for 2nd and 3rd line is should give newline(\n
or enter) immediately after giving delimiter for the third field. Can anyone help me how to get length as 4 for 2nd and 3rd line without changing the condition of the sample.txt
file and how to print the values of de[2]
(showing an arrayindexoutofbound exception)?