[英]ArrayIndexOutOfBoundsException for String.split()
This is the code for reading input from a file which contains student details in the form roll,name,age,street,city,zipcode
.这是从包含学生详细信息的文件中读取输入的代码,格式为
roll,name,age,street,city,zipcode
。 Few values among these can be null
even.其中很少有值可以为
null
。
For the following code, I am getting java.lang.ArrayIndexOutOfBoundsException: 1
对于以下代码,我收到
java.lang.ArrayIndexOutOfBoundsException: 1
Code is as follows-代码如下——
BufferedReader br=new BufferedReader(new FileReader(fileName));
while((line=br.readLine())!=null){
split_array=line.split("\\,");
String roll1=split_array[0];
String name=split_array[1];// This is the line which causes Exception
String age1=split_array[2];
String street=split_array[3];
String city=split_array[4];
String zip=split_array[5];
}
If you have empty lines in the middle of the file, you should如果文件中间有空行,你应该
if (line.equals(""))
continue;
or或者
if (split_array.length <= 1)
continue;
after calling split()
调用
split()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.