英文:
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<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;
}
}
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<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.
答案2
得分: 0
首先,您在for
循环中的条件是错误的:i <= i
始终为true
。<br>
其次,您必须为所有分支提供返回语句。例如,如果for
循环中的条件为false
,则在剩余的函数体中没有return
语句。
您应该:
- 在
for
循环之前放置cellList
- 在
for
循环之外放置return
语句
英文:
Firstly, your condition in for
loop is wrong: i <= 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
beforefor
loop - put
return
statement outsidefor
loop
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论