The error:
missing return statement
Or in free translation:
missing return statement
Occurs when a method is declared with return and there are possibilities that this return value will not be reached.
In your case the method is declared with the return of a String
( public String adicionar (String nome) {
) and in its else
this return is being respected. However, in% with_% there is no return, that is, when the if
condition is executed no getNumLugares() < getNumDePassageiros()
happens, disrespecting the declaration.
The way to solve your problem, roughly speaking, is to add the return
statement to return
, which can be done as follows:
public String adicionar(String nome) {
if (getNumLugares() < getNumDePassageiros()) {
nomes.add(nome);
return nome;
} else {
return "Não há lugares para mais passageiros";
}
}
But it is important to note that everything depends on how your method is executed, which may require that the return be done differently. You can return a status:
return "Passageiro adicionado.";
You can return null if there is no comment:
return null;
Or, as in the example, return the passenger name added:
return nome;
Perhaps in your case it is interesting to break the method in more parts, with a validation returning a id
, however it is as it was said previously: It depends on the application that will be given to the method.