简体   繁体   English

如何使用Java在Excel工作表的列中找到最大值?

[英]How to find maximum value in a column of an Excel sheet using java?

The image displays my dataset 该图像显示了我的数据集

I want to find the maximum value in the first column, but the code below shows an error. 我想在第一列中找到最大值,但是下面的代码显示错误。

Bus1.java:52: error: bad operand types for binary operator '>=' if(numbusarray.get(row)>=max); Bus1.java:52:错误:二进制运算符'> ='的错误操作数类型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); ^第一种类型:字符串第二种类型:int Bus1.java:53:错误:不兼容的类型:字符串不能转换为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

}

Your numbusarray contains strings and you are trying to compare string with int . 您的numbusarray包含字符串,并且您尝试将stringint进行比较。 You should first convert to int like this: 您首先应该这样转换为int:

int intNumber = Integer.parseInt(YourString);

And always make sure your String can be converted to int otherwise it will raise a NumberFormatException , you can also output your numbusarray contents to check. 并始终确保将String可以转换为int,否则将引发NumberFormatException ,还可以输出numbusarray内容进行检查。

Although sometimes Java can help you convert types but for beginners it's better to explicitly convert types and I believe this will save you a lot of time. 尽管有时候Java可以帮助您转换类型,但是对于初学者来说,最好是显式转换类型,我相信这将为您节省很多时间。

For adding things to List , you should put the appropriate type objects into it. 为了将东西添加到List ,您应该将适当的类型对象放入其中。 That's what your 那就是你的

List<String> numcommutersarray

wants, it contains the String type objects. 需要,它包含String类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM