I have tried various ways of removing the index out of bound error by changing the upper limits of the array.But the error persists. Where am I going wrong?
My program reads values(all rows) in the first column of excel sheet and finds the maximum value. Then based on the maximum value,criteria are formulated and the values are classified as Low,Medium,High and written back into a new excel sheet.
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
}
It is because your sh Sheet.class object doesn't have cells with column = 4. This should fix it:
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());
}
LAST EDIT:
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);