Problem with return of a Java method

Asked

Viewed 134 times

6

This method is giving problem in the return. An error message is appearing with the following statement Missing Return statement. Could someone tell me a solution?

public String adicionar (String nome) {
    if(getNumLugares() < getNumDePassageiros())
        nomes.add(nome);
    else 
        return "Não ha lugares para mais passageiros";
 }
  • What should be returned in case the addition succeeds?

  • 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).

2 answers

12

His method, due to if, has two possible paths for execution to follow.

  • 1st path, if the condition is true

    if(getNumLugares() < getNumDePassageiros())
        nomes.add(nome);
    
  • 2nd path, if the condition is false

    return "Não ha lugares para mais passageiros";
    

A method whose signature indicates that it must return a value must use, in all possible execution paths, the word return to finalize this path.

In your code, the error happens because in the first path was not used the return.

Change to include the return.

public String adicionar (String nome) {
    if(getNumLugares() < getNumDePassageiros()){
        nomes.add(nome);
        return "Adicionado"; //"Adicionado" ou outra coisa qualquer
    }
    return "Não há lugares para mais passageiros";
}

Note: You use a string as a return value to indicate the success or not of the execution of the same which may not be the best approach.

8

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.