我想写一个基本的java程序,它从Excel电子表格收集数据并将其存储在数组中。我遇到的问题是我得到了ArrayOutOfBounds异常,但看不到我的数组边界以外的位置。我甚至故意尺寸阵列比循环终止值显著较大:java.lang.ArrayIndexOutOfBoundsException:难以解决发生错误的地方
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class InputArray {
public static void main(String[] args) {
String[][] InputArray1;
int i = 0;
int j = 0;
InputArray1 = new String[220][220];
try{
Workbook OLE1Shots = Workbook.getWorkbook(new File("C:/Book12.xls"));
Sheet inList = OLE1Shots.getSheet(0);
for (i = 0; i < 190; i++){
Cell aCell = inList.getCell(i, 0);
Cell bCell = inList.getCell(i, 1);
String strIn = aCell.getContents();
String strOrd = bCell.getContents();
if (strIn != ""){
InputArray1[0][j] = strIn;
InputArray1[1][j] = strOrd;
j = j + 1;
}
}
}
catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (i = 0; i < 190; i++){
System.out.println(InputArray1[0][i]);
System.out.println(InputArray1[1][i]);
}
}
}
完整的消息我得到的是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
at InputArray.main(InputArray.java:29)
将是巨大的,如果有人能制定出什么错这里。希望它会变成只是我愚蠢。
编辑:
我在做一些愚蠢的事。抛出错误的是我用VBA符号表示单元格位置(行,列),其中jxl使用(列,行)。我改变了这些行:
for (i = 0; i < 190; i++){
Cell aCell = inList.getCell(i, 0);
Cell bCell = inList.getCell(i, 1);
这些:
c = inList.getRows();
for (i = 0; i < c; i++){
Cell aCell = inList.getCell(0, i);
Cell bCell = inList.getCell(1, i);
和代码现在运行和正确地打印出字符串。不过,我不断收到消息
InputArray as localhost contains obsolete methods. The virtual machine was unable to remove all stack frames running old code from the call stack...
我认为调试你的代码会有帮助... – TheLostMind
在“for”循环之前检查列表的大小。不要试图超越这一点。 –
在工作表范围是24 x 2数据单元格,所以我试着减少循环限制到i <10作为测试,但我仍然收到错误。当我调试时,如The Lost Mind建议的那样,我收到一条消息:“源附件不包含文件SheetImpl.class的源文件”。我尝试重新下载jxl,但我仍然收到此消息。 – Orphid