Of course, that second part of the if statement is completely false anyway. That is what the for loop is for.
public static int nameSearch(String[] inOrder, String searchedFor){
int returnVal = -1;
if (searchedFor == null) {
// if searchedFor is null the if in the for loop will have problems.
return returnVal;
}
for (int i = 0; i < inOrder.length; i++) {
if (searchedFor.equals(inOrder[i])) {
returnVal = i;
}
}
return returnVal;
}
P.S. You can't set an int to null anyway. ;-)
And, don't use == to compare Strings. ;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
It's perfectly fine to have return statements within if() blocks. The issue here is that there is not a guaranteed return path from the method. For instance, in the original code if inOrder.length is 0, there is no return. The compiler requires a valid return path regardless of any conditional statements and that is why you could not compile it.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
It's perfectly fine to have return statements within if() blocks. The issue here is that there is not a guaranteed return path from the method. For instance, in the original code if inOrder.length is 0, there is no return. The compiler requires a valid return path regardless of any conditional statements and that is why you could not compile it.
Yes, you can return from an if.However, if the only return statement you have is contained within an if statement, and the compiler cannot confirm that every possible avenue has been covered, you will still get a "missing return statement".
i.e.
public String aMethod() {
int x = 0;
//do something with x
if (x == 0) {
return "Hello";
}
}
will cause the compiler to throw that error, as the compiler cannot confirm that something will be returned. If you are going to return from an if statement, then return from every block in the if statement and include an else that also has a return statement or have a return statement at the end of the method.
i.e.
public String aMethod() {
int x = 0;
//do something with x
if (x == 0) {
return "Hello";
} else if (x == 1) {
return "GoodBye";
} else {
return "Bogus";
}
}
// or
public String aMethod() {
int x = 0;
//do something with x
if (x == 0) {
return "Hello";
}
return "Bogus";
}
// or
public String aMethod() {
int x = 0;
//do something with x
if (x <= 0) {
return "Hello";
} else if (x > 0) { // the same as a simple else, in this case
return "GoodBye";
}
}
In other words, every possible branchmust be covered in such a way that the compiler can determine that it is covered.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
That is exactly what I was trying to say :)
Perhaps the language wasn't clear.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Just wanted to make sure the OP understood. ;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Ah, ok. I thought maybe what I wrote, while seeming clear to me, was far from it to anyone else :P
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
If you indent the code properly you will see what's wrong - it's all to do with the {}
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073