问题描述
我正在尝试读取Excel文件,但似乎无法使我的代码正常工作。它返回的错误必须超出Array的范围,但是不管它是什么数字都行不通。有什么提示我的代码不正确吗?
注意:尝试仅读取 Price 列,并确保值不为NULL,不适用,小于80和大于130。
SpreadSheet:
boolean flag = true;
File inputF = new File("data.xlsx")
InputStream getStream = new FileInputStream(inputF);
try{
if(getStream.read() != -1){
BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
String line;
while((line = reader.readLine()) != null){
String[] csvs = line.split(",");
for (String str : csvs){
if (str != null && (str.equals("NA") || str.length() == 0)) {
System.out.println(str);
flag = false;
break;
}
}
if (!flag){
break;
}else{
String Price = csvs[1]; //TODO replace with price index
price = price.trim();
Integer priceValue = new Integer(Price);
if (priceValue != null && (priceValue < 80 || priceValue > 130)){
flag = true;
}
}
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(flag);
return flag;
错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
预期输出:(基于提供的电子表格)
True
解决方法
如评论中所述,您应该使用一个POI being an example库,因为您很可能会从excel中的工作表数量中得到错误。