Reputation: 85
public double open() {
if (status.equalsIgnoreCase("Pending")) {
status = "Open";
if (startPrice <= 5) {
return 0.2;
} else if (startPrice > 5 && startPrice <= 20) {
return 0.5;
} else if (startPrice > 20 && startPrice <= 100) {
return 1;
} else if (startPrice > 100 && startPrice <= 250) {
return 2.5;
} else if (startPrice > 250) {
return 5;
}
} else if (!status.equals("Pending")) {
return -1;
}
}
Can you explain for my why the compiler keep asking missing return statements. And how can i fix it
Upvotes: 2
Views: 139
Reputation: 7389
It is complaining because all your return statements are in if statements
. Just delete the else if
and return -1 at the end.
Upvotes: 1
Reputation: 1846
It's all to do with what the return statement is local to. If all of the if statements were false then the method wouldn't return anything because all of the return statements are local to the if statements. The program can't know that the status would otherwise be pending, thus the compiler complains.
public double open() {
if (status.equalsIgnoreCase("Pending")) {
status = "Open";
if (startPrice <= 5) {
return 0.2;
} else if (startPrice > 5 && startPrice <= 20) {
return 0.5;
} else if (startPrice > 20 && startPrice <= 100) {
return 1;
} else if (startPrice > 100 && startPrice <= 250) {
return 2.5;
} else if (startPrice > 250) {
return 5;
}
return -1; // no need for an else statement since if an one if statement returned true it wouldn't execute the rest of the code
}
Upvotes: 0
Reputation: 7812
Java is not smart enough to determine that you're guaranteed to return. You need to explicitly tell it by having an else on your ifs, or having return at the end to catch any flow through.
In this case, your if statements were redundant as well. The cleaned-up and working version would look as follows:
public double open() {
if (status.equalsIgnoreCase("Pending")) {
status = "Open";
if (startPrice <= 5) {
return 0.2;
} else if (startPrice <= 20) {
return 0.5;
} else if (startPrice <= 100) {
return 1;
} else if (startPrice <= 250) {
return 2.5;
} else {
return 5;
}
}
return -1;
}
else if (startPrice > 5 && startPrice <= 20)
When you got to this line, you already knew that startPrice > 5
. If it wasn't then it would have entered the previous if block.
Upvotes: 1
Reputation: 26094
Do like this
public double open() {
if (status.equalsIgnoreCase("Pending")) {
status = "Open";
if (startPrice <= 5) {
return 0.2;
} else if (startPrice > 5 && startPrice <= 20) {
return 0.5;
} else if (startPrice > 20 && startPrice <= 100) {
return 1;
} else if (startPrice > 100 && startPrice <= 250) {
return 2.5;
} else if (startPrice > 250) {
return 5;
}
}
return -1;
}
Upvotes: 2
Reputation: 234715
The compiler doesn't know that status.equalsIgnoreCase("Pending")
and !status.equals("Pending")
cover all the possibilities. In fact, I'm only guessing that from inspecting the function names.
You need to help it out.
One fix - if this is your intention - is to write
} else /*if (!status.equals("Pending"))*/ {
return -1;
}
Upvotes: 3
Reputation: 68935
What if neither
is true?
You need one more return statement.
Upvotes: 1
Reputation: 6982
change the last if else
to just else
to provide a fallback solution. You're providing another if
condition with if else
, so there is no branch if this condition is false, too.
Upvotes: 0
Reputation: 19284
This happens because if :
status.equals("Pending")
returns true
, you return nothing.
You can use:
if (status.equalsIgnoreCase("Pending")) {
....
} else {
return -1;
}
Upvotes: 0