error: This method must return a result of type int
Hello, I am getting this error with my code "error: This method must return a result of type int" haven't a clue why it's happening.
Code Java:
int processresponse(String response){
int a;
Boolean foundresponse = false;
for (a = 0; a < vresponses.size(); a++)
{ // Check if response exists.
if (vresponses.get(a) == response)
{
foundresponse = true;
return a;
}
}
if (foundresponse == false)
{
vresponses.add(response);
return vresponses.size()-1;
}
}
Any help much appreciated.
Re: error: This method must return a result of type int
Code java:
public int processResponse(String response) {
int a;
boolean foundResponse = false;
for (a = 0; a < vresponses.size(); a++) { // Check if response exists.
if (vresponses.get(a) == response) {
foundResponse = true;
return a;
}
}
if (!foundResponse) {
vresponses.add(response);
return (vresponses.size() - 1);
}
return (vresponses.size() - 1); //Must return Integer here..
}
You were missing a return statement as you were only returning values if certain conditions were true, and your method always needs an Integer to be returned.
Re: error: This method must return a result of type int
Thanks...
So is "(!foundResponse)" the same as "(foundResopnse == false)"?
Re: error: This method must return a result of type int
Yeah it is.
Id say its better convention than ==false/true.
The same goes for true.
Code java:
if (isTrue) {
//do something if isTrue == true
} else if (!isTrue) {
//do something if isTrue == false}
}
Re: error: This method must return a result of type int
Also, I think you're looking for
Code java:
if (vresponses.get(a).equals( response))
{
foundresponse = true;
return a;
}
Actually, why it was giving that error message, I'm not quite sure. If it wasn't found, it would return the new index. If it was, it would return that index.
The only thing I can think of is that it wanted a () around your second return statement.
Actually that would sorta make sense.
Otherwise it might be confused on what to return
For instance
(int) Math.random() * 5
Would be returning an int of Math.random(), not an int of Math.random() * 5.
Re: error: This method must return a result of type int
Also, I might add that there's no need to use the Boolean wrapper class. I think that's only if you needed to either store the Boolean in like a Vector, ArrayList, Set, Map, Tree, or Linked List, or some other type of Collection. Or if you needed to make it turn into a String, like for a TextField, textArea, etc. I can't think of any other uses other than those two I mentioned above. (However, perhaps, I never came across this yet, it might possibly be to use the equals(Object obj) method to check to see if two booleans were equal to each other. However, I believe boolean1 == boolean2 is also valid and preferable. ) You should probably use the primitive boolean.
Re: error: This method must return a result of type int
Actually newbie is quite correct.
Java doesn't expect you to make needless checks, so it assumes that if blocks won't necessarily execute.
Re: error: This method must return a result of type int
If it finds it, foundResponse is set to true, but that doesn't matter as the return statement will return a and exit the method if it finds it. If it never gets set to true, it'll go into the if loop happens if it can't find it. The third return statement doesn't make much sense.
Also, it should be .equals(response).