[英]Array index out of bounds except error in Java
I'm trying to read in a CSV file and keep getting the error:我正在尝试读取 CSV 文件并不断收到错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0
at dataExpert.Control.main(Control.java:22)在 dataExpert.Control.main(Control.java:22)
I've tried the following:我尝试了以下方法:
String split_by = ",";
BufferedReader buff_reader = new BufferedReader(new FileReader("file path"));
String line;
while((line = buff_reader.readLine()) != null){
if(line.contains(",")){
String[] b = line.split(split_by);
System.out.println(b[0]);
}
This is likely caused by a row with only commas.这可能是由仅包含逗号的行引起的。 When you call split(",")
, trailing empty strings are discarded, which leaves you with an empty array.当您调用split(",")
,会丢弃尾随的空字符串,从而为您留下一个空数组。 One solution is to call split(",", -1)
, which preserves trailing empty strings.一种解决方案是调用split(",", -1)
,它保留尾随空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.