The mistake:
Missing Return statement
Or in free translation:
return declaration missing
It occurs when a method is declared with return and there are possibilities of this return value not being reached.
In your case the method is declared with the return of a String
(public String adicionar (String nome) {
) and in your else
this return is being respected. However if
there is no return, ie when the condition getNumLugares() < getNumDePassageiros()
no one is executed return
happens, disregarding the statement.
The means to solve your problem, roughly speaking, is to add the instruction return
within the id
, which may be carried out as follows:
public String adicionar(String nome) {
if (getNumLugares() < getNumDePassageiros()) {
nomes.add(nome);
return nome;
} else {
return "Não há lugares para mais passageiros";
}
}
However, it is important to note that it all depends on how your method is executed, which may require that Return be done differently. You can return a status:
return "Passageiro adicionado.";
You can return null if there is no observation:
return null;
Or, as in the example, return the added passenger name:
return nome;
Perhaps in your case it is interesting to break the method into more parts, with a validation returning a Boolean
, but it is as I said before: It depends on the application that will be given to the method.
What should be returned in case the addition succeeds?
– Victor Stafusa
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack