Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在我的程序中删除索引越界错误?_Java_Excel_Jxl - Fatal编程技术网

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

Java 如何在我的程序中删除索引越界错误?,java,excel,jxl,Java,Excel,Jxl,我尝试过通过更改数组的上限来消除索引越界错误的各种方法。但是错误仍然存在。我哪里做错了 我的程序读取excel工作表第一列中的值(所有行),并找到最大值。然后根据最大值,制定标准,将值分为低、中、高,并写回新的excel表格 import java.io.FileInputStream; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff

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

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

}
import java.io.FileInputStream;
导入java.io.IOException;
进口jxl.Cell;
进口jxl.Sheet;
导入jxl.工作簿;
导入jxl.read.biff.BiffException;
导入java.io.*;
导入java.util.*;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入jxl.write.Label;
导入jxl.write.WriteException;
公务舱巴士3{
List numbusarray=new ArrayList();
List numcommutersarray=new ArrayList();
List numcommercialarray=new ArrayList();
静态可写工作簿;
静态可写表wSheet;
public void readExcel()抛出biffeException、IOException、WriteException//方法来读取excel中的内容
{
字符串FilePath=“Bus1.xls”;
扫描仪sc=新的扫描仪(System.in);
int max=0;
FileInputStream fs=新的FileInputStream(FilePath);
工作簿wb=Workbook.getWorkbook(fs);
Sheet sh=wb.getSheet(“Bus1”);//获取对该工作表的访问权限
int totalNoOfRows=sh.getRows();//获取工作表中的行数
int totalNoOfCols=sh.getColumns();//获取工作表中的列数
System.out.println(totalNoOfRows);
//将每列中的excel内容添加到arraylist

对于(int row=1;row来说,这看起来不是一个非常复杂的问题。 索引越界意味着您试图访问数组中不存在的位置。
注意您的
numbusarray
变量,可能
被设置为无效索引。

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

这是因为您的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
上次编辑:

    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);
for(int row=1;rowmax){
max=整数;
System.out.println(“max:+max”);
}
}
}
系统输出打印项次(最大值);
工作簿=工作簿.createWorkbook(新文件(“sampletestfile.xls”);
WritableSheet wSheet=workbook.createSheet(“名称”,0);

这是因为您的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
上次编辑:

    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);
for(int row=1;rowmax){
max=整数;
System.out.println(“max:+max”);
}
}
}
系统输出打印项次(最大值);
工作簿=工作簿.createWorkbook(新文件(“sampletestfile.xls”);
WritableSheet wSheet=workbook.createSheet(“名称”,0);

调试它,否则将堆栈tracestart行以0发布。不要从1开始。所有这些初始化i=1的for循环,使其i=0。然后尝试
row
将大小减小2。检查此情况。我从1开始大部分for循环,因为我需要从excel工作表st读取条目arts从第一行开始。@Farazdurrani在excel工作表中有15行(totalNoOfRows),我必须从第一行开始读取。因此for循环的范围为1-14(14项)。然后对于最大函数,for循环的范围为1-13,因为我最初将第一项指定为“max”。最后一个for循环的范围为0-13(14项)@MaverickDebug it,否则将堆栈tracestart行以0发布。不要从1开始。所有这些对于初始化i=1的循环,将它们设为i=0。然后尝试
row
将大小减少2。检查此c