我得到错误,尽管我已经添加了返回语句,但仍需要添加返回语句。

huangapple go评论68阅读模式
英文:

I get the error that I must add return statement although I have added the return statement

问题

public static ArrayList<Double> readSheet(XSSFSheet spreadsheet) {
    int rows = spreadsheet.getPhysicalNumberOfRows();

    for (int i = 0; i <= i; i++) {
        XSSFRow row = spreadsheet.getRow(i);

        if (row == null) {
            continue;
        }

        int cells = row.getPhysicalNumberOfCells();
        ArrayList<Double> cellList = new ArrayList<>();

        for (int j = 0; j < cells; j++) { //遍历每一列
            XSSFCell cell = row.getCell(j);
            cell.setCellType(cell.CELL_TYPE_STRING);

            //new Double("1.0").intValue()
            Double cellValue = cell.getNumericCellValue();
            cellList.add(cellValue);
        }

        return cellList;
    }
}
英文:
public static ArrayList&lt;Double&gt; readSheet(XSSFSheet spreadsheet) {
	int rows = spreadsheet.getPhysicalNumberOfRows();
		
	for (int i = 0; i &lt;= i; i++) {
		XSSFRow row = spreadsheet.getRow(i);
		
		if (row == null) {
			continue;
		}
		
		int cells = row.getPhysicalNumberOfCells();
		ArrayList&lt;Double&gt; cellList = new ArrayList&lt;&gt;();
	
		for (int j = 0; j &lt; cells; j++) { //遍历每一列
			XSSFCell cell = row.getCell(j);
			cell.setCellType(cell.CELL_TYPE_STRING);
				
			//new Double(&quot;1.0&quot;).intValue()
			Double cellValue = cell.getNumericCellValue();
			cellList.add(cellValue);		
		}
			
		return cellList;
	}
}

Can someone please help

答案1

得分: 2

You should provide the return statement outside of your for loop:

public static ArrayList<Double> readSheet(XSSFSheet spreadsheet) {
    int rows = spreadsheet.getPhysicalNumberOfRows();
    
    ArrayList<Double> cellList = new ArrayList<>();
    
    for (int i = 0; i <= i; i++) {
        ...
    }
    return cellList;
}

Because the compile cannot decide if there is any run of your for loop and therefore not guarantee that your return statement will be reached.

This also means that you need to declare your cellList variable outside of the for loop.

And please also check your loop condition. While i<=i might be always true you don't want to loop indefinitely.

英文:

You should provide the return statement outside of your for loop:

public static ArrayList&lt;Double&gt; readSheet(XSSFSheet spreadsheet) {
    int rows = spreadsheet.getPhysicalNumberOfRows();
    
    ArrayList&lt;Double&gt; cellList = new ArrayList&lt;&gt;();
    
    for (int i = 0; i &lt;= i; i++) {
        ...
    }
    return cellList;
}

Because the compile cannot decide if there is any run of your for loop and therefore not guarantee that your return statement will be reached.

This also means that you need to declare your cellList variable outside of the for loop.

And please also check your loop condition. While i&lt;=i might be always true you don't want to loop indefinitely.

答案2

得分: 0

首先,您在for循环中的条件是错误的:i &lt;= i始终为true。<br>
其次,您必须为所有分支提供返回语句。例如,如果for循环中的条件为false,则在剩余的函数体中没有return语句。

您应该:

  • for循环之前放置cellList
  • for循环之外放置return语句
英文:

Firstly, your condition in for loop is wrong: i &lt;= i is always true. <br>
Secondly, you must provide return statement for all branches. For example, if condition in for loop will be false, there is no return statements in remaining function body.

You should:

  • put cellList before for loop
  • put return statement outside for loop

huangapple
  • 本文由 发表于 2020年8月4日 21:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63247473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定