我想在第一列中找到最大值,但下面的代码显示错误。
Bus1.java:52:错误:二元运算符的错误操作数类型'> =' 如果(numbusarray.get(行)> =最大值); ^第一种类型:字符串第二种类型:int Bus1.java:53:错误:不兼容的类型:字符串 无法转换为int 最大= numbusarray.get(行);
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
}
答案 0 :(得分:0)
您的 numbusarray 包含字符串,并且您尝试将字符串与 int 进行比较。您应该首先转换为int,如下所示:
int intNumber = Integer.parseInt(YourString);
始终确保您的String可以转换为int,否则会引发 NumberFormatException ,您也可以输出 numbusarray 内容进行检查。
虽然有时Java可以帮助您转换类型,但对于初学者来说,明确转换类型会更好,我相信这会为您节省大量时间。
要将内容添加到列表,您应该将相应的类型对象放入其中。这就是你的
List<String> numcommutersarray
想要,它包含 String 类型对象。