繁体   English   中英

如何在我的程序中删除索引出界错误?

[英]How to remove the index out of bound error in my program?

我尝试了各种方法来通过更改数组的上限来消除索引错误,但是错误仍然存​​在。 我要去哪里错了?

我的Excel工作表的屏幕截图

我的程序读取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

}

它看起来不是一个非常复杂的问题。 索引超出范围意味着您正在尝试访问数组中不存在的位置。 注意您的numbusarray变量,可能row被设置为无效索引。

这是因为您的sh Sheet.class对象没有具有column = 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM