如何删除我的程序中的绑定错误的索引?

我已经尝试了通过改变数组的上限来消除索引出错的各种方法,但错误依然存在。 我哪里错了?

我的Excel表格的屏幕截图

我的程序读取excel表格第一列中的值(所有行)并find最大值。 然后根据最大值制定标准,将这些值分为低,中,高,并写回新的Excel表格。

import java.io.FileInputStream; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import java.io.*; import java.util.*; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.Label; import jxl.write.WriteException; public class Bus3{ List<String> numbusarray = new ArrayList<String>(); List<String> numcommutersarray = new ArrayList<String>(); List<String> numcommercialarray = new ArrayList<String>(); static WritableWorkbook workbook; static WritableSheet wSheet; public void readExcel() throws BiffException, IOException, WriteException//method to read contents form excel { String FilePath = "Bus1.xls"; Scanner sc = new Scanner(System.in); int max=0; FileInputStream fs = new FileInputStream(FilePath); Workbook wb = Workbook.getWorkbook(fs); Sheet sh = wb.getSheet("Bus1");// TO get the access to the sheet int totalNoOfRows = sh.getRows();// To get the number of rows present in sheet int totalNoOfCols = sh.getColumns();// To get the number of columns present in sheet System.out.println(totalNoOfRows); //adding excel contents from every column to arraylist for (int row = 1; row <totalNoOfRows; row++) { numbusarray.add(sh.getCell(2, row).getContents()); } for (int row = 1; row <totalNoOfRows; row++) { numcommutersarray.add(sh.getCell(3, row).getContents()); } for (int row = 1; row <totalNoOfRows; row++) { numcommercialarray.add(sh.getCell(4, row).getContents()); } //to find maximum of numbusarray max=Integer.parseInt(numbusarray.get(0)); for (int row = 1; row < totalNoOfRows-1; row++) { if(!(numbusarray.get(row)).isEmpty()) { int intNumber=Integer.parseInt(numbusarray.get(row)); if(intNumber>max) { max=intNumber; //System.out.println(max); } } } System.out.println(max); WritableWorkbook workbook = Workbook.createWorkbook(new File("sampletestfile.xls")); WritableSheet wSheet = workbook.getSheet(0); int increment=max/3; int a=increment; int b=a+increment; int c=b+increment; for (int row = 0; row < totalNoOfRows-1; row++) { if(!(numbusarray.get(row)).isEmpty()) { int compare=Integer.parseInt(numbusarray.get(row)); if(compare<=a) {Label label= new Label(0, row, "Low");//column,row,strngdata wSheet.addCell(label);} else if((compare>a)&&(compare<=b)) {Label label= new Label(0, row, "Medium");//column,row,strngdata wSheet.addCell(label);} else {Label label= new Label(0, row, "High");//column,row,strngdata wSheet.addCell(label);} } } /*Iterator itr=numbusarray.iterator(); //to print arraylist demo while(itr.hasNext()){ System.out.println(itr.next()); }*/ }//end of method to read contents from excel //to close file public static void closeFile() { try { // Closing the writable work book workbook.write(); workbook.close(); // Closing the original work book } catch (Exception e) { e.printStackTrace(); } } public static void main(String args[]) throws BiffException, IOException, WriteException //main class { Bus3 DT = new Bus3(); DT.readExcel(); Bus3.closeFile(); }//end of main class } 

这是因为你的SH Sheet.class对象没有列= 4的单元格。这应该修复它:

  for (int row = 1; row < totalNoOfRows; row++) { numbusarray.add(sh.getCell(1, row).getContents()); } for (int row = 1; row < totalNoOfRows; row++) { numcommutersarray.add(sh.getCell(2, row).getContents()); } for (int row = 1; row < totalNoOfRows; row++) { numcommercialarray.add(sh.getCell(3, row).getContents()); } 

最后编辑:

  for (int row = 1; row < totalNoOfRows; row++) { numbusarray.add(sh.getCell(1, row).getContents()); } for (int row = 1; row < totalNoOfRows; row++) { numcommutersarray.add(sh.getCell(2, row).getContents()); } for (int row = 1; row < totalNoOfRows; row++) { numcommercialarray.add(sh.getCell(3, row).getContents()); } // to find maximum of numbusarray max = 0; for (int row = 1; row < totalNoOfRows; row++) { if (!(numbusarray.get(row - 1)).isEmpty()) { int intNumber = Integer.parseInt(numbusarray.get(row - 1)); if (intNumber > max) { max = intNumber; System.out.println("max: " + max); } } } System.out.println(max); workbook = Workbook.createWorkbook(new File("sampletestfile.xls")); WritableSheet wSheet = workbook.createSheet("name", 0); 

这看起来不是一个非常复杂的问题。 索引越界意味着您正在尝试访问数组中不存在的位置。 注意你的numbusarrayvariables,可能row被设置为无效索引。