Return method asks return that has already been given

Asked

Viewed 99 times

0

I have the following class:

public class Exemplar {
    private boolean disponivel;
    private int codigo;
    // seus setters e getters
}

and this other:

public class Biblioteca {
    private ArrayList<Exemplar> exemplares;

    public boolean emprestar(int codigo){
       for(Exemplar e : exemplares){
           if (e.getCodigo() == codigo){
               return e.isDisponivel();
           }else{
               return false;
           }
       }
    }
}

This method emprestar() is giving the error of

"Missing Return statement"

and I’ve already defined the return within the for, what’s wrong with?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

2

What’s wrong is missing one return.

A method that requires a return has to have in all possible execution paths the corresponding return

Case exemplary no for(foreach) entries are not executed, and no Return is found.

Add a Return at the end of the foreach block:

public class Biblioteca {
    private ArrayList<Exemplar> exemplares;
    public boolean emprestar(int codigo) {
       for (Exemplar e : exemplares) {
           if (e.getCodigo() == codigo) return e.isDisponivel();
           else return false;
       }
       return false;
    }
}
  • But this way he’ll only do an interaction because of return false and the decision is not satisfied?

  • @Virgilionovic I just answered the question. Anything else I added would be "kick".

  • Your code once does the for that is my question!

  • 1

    @Virgilionovic I understand your question, it was the first thing I noticed when I analyzed the code. However, that might be what AP wants the code to do, I don’t know.

  • It was a warning only. I understand I just really wanted to help

1

And if you don’t even get on for? This mechanism will only be executed if there is an element in the collection, if the data collection is empty or enters the loop, then it goes to the end of the method and what is it returning? Nothing! There’s one missing return in this case and that makes sense, then "it seems" that false is suitable but not sure without knowing the requirements.

The for is still a conditional execution (branch), then it needs to be considered. You have to consider all possible paths when giving a return in the method. Something like this could solve:

public class Biblioteca {
    private ArrayList<Exemplar> exemplares;
    public boolean emprestar(int codigo) {
       for (Exemplar e : exemplares) {
           if (e.getCodigo() == codigo) return e.isDisponivel();
           else return false;
       }
       return false;
    }
}

I think all the code has other problems, but I can only solve this one. An example is that this will only evaluate the first item of the data collection, no matter what happens it will terminate the method after the first item, so I think you put the return randomly misplaced, cannot place a return anywhere, you might want to do this:

public class Biblioteca {
    private ArrayList<Exemplar> exemplares;
    public boolean emprestar(int codigo) {
       for (Exemplar e : exemplares) if (e.getCodigo() == codigo) return e.isDisponivel();
       return false;
    }
}

I put in the Github for future reference.

This way if you find a code that matches what you are looking for it returns if it is available or not according to the method isDisponivel(), but if you can’t find the code, that is, go through everything and no code is the same, then surely you should return a false one because if you don’t even have the code, it is certainly unavailable.

The method name seems wrong, it is doing a check is not lending anything.

  • But this way he’ll only do an interaction because of return false and the decision is not satisfied? (its first code)

  • That’s why I made the second one. I can’t get away with what he wants, there’s no requirement. My second code makes it the same as yours but much simpler.

  • Simple !!! Or a code that is often difficult to read. Their code are hard to read people who do not know how to program it is difficult to understand mine is more didactic and it has no problem to do so. Yours is correct but difficult to understand for those who can not program

  • I don’t make code for people who can’t program, I help people learn to program. Maintaining the status quo is not my goal, I want to see people grow. Created unnecessary state, extra deviation is complexity, it’s complicated code and everyone who has learned to program really knows it, some people have stood still in what they learned the first time and only see like this. I’ll always show you the best way to do it. People choose what they want to follow.

0

The return by error showing need to exist at the end of the method, so change your code like this:

public class Biblioteca {
    private ArrayList<Exemplar> exemplares;

    public boolean emprestar(int codigo) {
       boolean status = false;
       for(Exemplar e : exemplares){
           if (e.getCodigo() == codigo) {
               status = e.isDisponivel();
               break;
           }
       }
       return status;
    }
}

It is worth remembering that this refers to satisfy or not a condition and generally (it is not mandatory, but in this case it is) the return must be the last code in any return method. Exists return which is placed in the middle of a flow, but for this at the end of the method needs to have some return if it does not satisfy some comparison or structure.

In that case the for after the comparison of if if the value for the variable is passed status and then right after to the flow with break, so that from gives li leaves this repeat structure and soon after return the value.

I believe you need to go through to satisfy the comparison of if if you won’t return false. There’s something wrong with that code and if the e.getCodigo() exist and the e.isDisponivel() for false you will never really know if the code was not found or if it was found and your isDisponivel() is also false.

Wouldn’t it be better to return a value char, a complex structure, a integer I don’t know, like 0 for not found, 1 for found but, status = false and 2 for found to status = true, would be an idea that really defines what this method really needs to return or even returns from the item that was found (exemplar) if it’s not null, of course it depends on your context that is not well described in the question.

Browser other questions tagged

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