Reputation: 137
I have the following method, but I want to use recursion; however, I get an error: "missing return statement".
static String buscar(NodoDeArbol raiz, String letra) {
if(raiz == null) {
aux="";
for (int i = 0; i < auxiliar.length()-1; i++) {
aux+=auxiliar.charAt(i);
}
return aux;
}
auxiliar = buscar(raiz.izquierdo, auxiliar+= "0");
auxiliar = buscar(raiz.derecho, auxiliar+= "1");
}
What should to do to fix this?
Upvotes: 1
Views: 98
Reputation: 14666
You're if statement has the only return. You will need to add a return statement for the case that is not included in your if statement. Just judging by your current code, I'm guessing you meant to have return auxiliar;
at the end of your method.
Upvotes: 2
Reputation: 4643
There are a couple of errors
aux
(maybe is global?)raiz != null
Upvotes: 3
Reputation: 842
wrong is, that you return only when raiz is null, when you get parameter raiz, that is not null, method would never end - cause there is no return outside of if statement
Upvotes: 1
Reputation: 3509
You are only returning a string when the case is null, but not returning anything when the case is not null.
You need to handle all cases. A return method (String) MUST return some sort of value.
Upvotes: 2