Missing Return Statement [closed]
I am Trying to read an Excel file using and finding the Start position for my another for loop.
This is my code.
public static int CheckStartPosition(String filename,int Colno,String StartChar, int sheetno,int startpos) {
try {
Workbook workbook = new Workbook();
workbook.open(filename);
Worksheet worksheet = workbook.getWorksheets().getSheet(sheetno);
Cells cells=worksheet.getCells();开发者_如何学C
int num=cells.getMaxDataRow();
int num1=cells.getMaxDataColumn();
int numofsheet= workbook.getNumberOfSheets();
System.out.println(numofsheet);
for (int n1=0;n1<=num;n1++) {
Cell cell1=cells.getCell(n1,Colno);
if(cell1.getValue()!=null) {
String value =cell1.getValue().toString().toLowerCase();
if(value.equals(StartChar)) {
System.out.println( cell1.getValue());
int S= cell1.getRowIndex();
startpos=S+1;
System.out.println(startpos);
}
}else{}
}
workbook.save("C:\\Movies.xls",FileFormatType.EXCEL97TO2003);
return startpos;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
:-
CheckStartPosition(f1.xls, 0,"abc",2,0);
How to return Startposition
It is a little hard to read due to formattign. CheckStartPosition() needs to return an int. In your catch block you don't return anything. Return something. Or better yet declare CheckStartPosition with throws IOException and drop try catch from CheckStartPosition. Alternative use your own exception type.
You are catching an exception, but not returning anything. Try this:
catch (IOException e)
{
e.printStackTrace();
return -1; // Some special value that means "I exploded"
}
Or better, don't catch and declare your method to throws IOException
You don't return anything is you get an exception. It is likely a better approach is to declare the actual exceptions in the throws
clause of the method.
What you could do is declare the return-value before your try-catch block and give it -1. In your try-catch block you assign the value. After the try-catch block you return it.
int CheckStartPosition(...)
{
int returnInt = -1;
try{
...
returnInt = startpos; // instead of return startpos
...
}catch(...){
...
returnInt = -1; // if your code can crash after the "returnInt = startpos;"
}
return returnInt;
}
You have to call your method with something as follows :
int returnedValue = CheckStartPosition(f1.xls, 0,"abc",2,0);
精彩评论