The image displays my dataset如何在使用java的Excel工作表的列中查找最大值?
我想找到第一列中的最大值,但下面的代碼顯示一個錯誤。
Bus1.java:52: error: bad operand types for binary operator '>=' if(numbusarray.get(row)>=max); ^ first type: String second type: int Bus1.java:53: error: incompatible types: String cannot be converted to int max=numbusarray.get(row);
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.*;
public class Bus1{
List<String> numbusarray = new ArrayList<String>();
List<String> numcommutersarray = new ArrayList<String>();
List<String> numcommercialarray = new ArrayList<String>();
public void readExcel() throws BiffException, IOException//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
//adding excel contents from every column to arraylist
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(5, row).getContents());
}
for (int row = 1; row < totalNoOfRows; row++)
{
if(numbusarray.get(row)>=max);
max=numbusarray.get(row);
}
System.out.println(max);
Iterator itr=numbusarray.iterator(); //to print arraylist demo
while(itr.hasNext()){
System.out.println(itr.next());
}
}//end of method to read contents from excel
public static void main(String args[]) throws BiffException, IOException //main class
{
Bus1 DT = new Bus1();
DT.readExcel();
}//end of main class
}
您試圖在第一個錯誤中將字符串(來自'numbusarray.get(row)')與數字('max')進行比較。在第二個錯誤中,你試圖設置一個數字等於一個字符串。爲了解決這個問題; 'Double.parseDouble(numbusarray.get(row))' – marko5049
感謝您的回答。代碼已編譯,但顯示以下運行時錯誤,異常在線程「main」java.lang.IndexOutOfBoundsException:Index:1,Size :在java.util.ArrayList.rangeCheck(ArrayList.java:653) 在java.util.ArrayList.get(ArrayList.java:429) 在Bus1.readExcel(Bus1.java:42) 在總線1 .main(Bus1.java:57) – Zac
糾正了代碼....沒有更多的錯誤....謝謝 – Zac