2017-03-06 49 views
0

我已經嘗試了通過改變數組的上限來移除索引超出限制錯誤的各種方法,但是錯誤仍然存​​在。我哪裏錯了?如何刪除我的程序中的綁定錯誤的索引?

Screenshot of my excel sheet

我的程序讀取在Excel工作表的第一列的值(所有的行),並且得到的最大值。然後基於最大值制定標準,並將這些值分類爲低,中,高,並寫回到新的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 

} 
+0

調試它,否則請將您的堆棧跟蹤 –

+0

起始行的值設爲0.不要從1開始。所有這些for循環,您初始化i = 1,使它們i = 0.然後嘗試 –

+0

'row Maverick

回答

1

這是因爲你的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); 
+0

我還有前兩排。巴士的數量是我的第三列,所以我認爲我給出的指數2是正確的。 @Rinat – Zac

+0

也應該改變:max = 0; \t \t對(INT行= 1;行 Rinat

+0

而這個:w​​orkbook = Workbook.createWorkbook(new File(「sampletestfile.xls」)); \t \t WritableSheet wSheet = workbook.createSheet(「name」,0); – Rinat

1

它看起來不是一個非常複雜的問題。 索引越界意味着您正試圖訪問數組中不存在的位置。 請注意您的numbusarray變量,可能row正在設置爲無效索引。

+0

異常在線程 「主」 java.lang.IndexOutOfBoundsException:索引:0,大小:在java.util.ArrayList.rangeCheck( ArrayList.java:653) at java.util.Array在Bus3.readExcel(Bus3.java:69) (在Bus3.main(Bus3。))上的List.get(ArrayList.java:429) at jxl.write.biff.WritableWorkbookImpl.getSheet(WritableWorkbookImpl.jav a:399) java:115) – Zac

+0

以上是顯示的錯誤。 numbusarray數組包含14個術語。所以0-13是我給numbusarray的限制 – Zac