The image displays my datasetjavaを使用して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')を比較しようとしています。 2番目のエラーでは、数値を文字列に設定しようとしています。この使用を解決するには'Double.parseDouble(numbusarray.get(row))' – marko5049
答えていただきありがとうございます。コードがコンパイルされましたが、次の実行時エラーが表示され、スレッド "main"の例外java.lang.IndexOutOfBoundsException:Index:1、Size :Bus1.readExcel(Bus1.java:42) でjava.util.ArrayList.get(ArrayList.java:429) でjava.util.ArrayList.rangeCheck(ArrayList.java:653)で0 バス1で.main(Bus1.java:57) – Zac
コードを整理しました....もうエラーはありません....ありがとう – Zac