2017-03-02 26 views
0

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 

} 
+0

您試圖在第一個錯誤中將字符串(來自'numbusarray.get(row)')與數字('max')進行比較。在第二個錯誤中,你試圖設置一個數字等於一個字符串。爲了解決這個問題; 'Double.parseDouble(numbusarray.get(row))' – marko5049

+0

感謝您的回答。代碼已編譯,但顯示以下運行時錯誤,異常在線程「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

+0

糾正了代碼....沒有更多的錯誤....謝謝 – Zac

回答

0

numbusarray包含字符串和您要比較INT。你應該首先將其轉換爲int這樣的:

int intNumber = Integer.parseInt(YourString); 

並始終確保您的字符串可以轉換爲int否則將引發NumberFormatException異常,你也可以輸出你的numbusarray內容進行檢查。

儘管有時Java可以幫助您轉換類型,但對於初學者來說,最好明確地轉換類型,我相信這會爲您節省大量時間。

要添加東西到列表,你應該把適當的類型對象放入它。這就是你的

List<String> numcommutersarray 

想要的東西,它包含了字符串類型的對象。

+0

代碼已編譯,但顯示以下運行時錯誤,異常在線程「main」java.lang.IndexOutOfBoundsException :Index:1,Size:0 at java.util.ArrayList.rangeCheck(ArrayList.java:653)at java.util.ArrayList.get(ArrayList.java:429)at Bus1.readExcel(Bus1.java:42)at Bus1.main(Bus1.java:57) – Zac

+0

我按照建議添加了以下更改(int row = 1;行 = max); max = Integer.parseInt(numbusarray.get(row)); } System.out.println(max); – Zac

+0

糾正了代碼...沒有更多的錯誤...謝謝 – Zac

相關問題