Break inside method that returns Boolean

Asked

Viewed 558 times

4

I’m trying to make a loop for, who at a certain time returns true, and out of the loop.

Example:

public boolean autentica (Usuario usuario) {
    for(i=0; i<listaUsuarios.size ; i++){
        if(usuario.getSenha().equals(listaUsuarios.get(i).getSenha()){
            return true;
            break;
        }
    }
}

That is, when a certain thing happens, return true and exit the loop, because if not will continue testing the password on other users.

In short, I want to return true and get out of the loop. How do I do it? So it is wrong.

  • But as soon as you return, it leaves the function and the loop.

2 answers

6


If you want to give break and the return within the if, makes no sense. Can’t put anything after a return. If the problem is just the error that is occurring, just take the break because the only problem is having a command after the method output. When you exit the method, you certainly came out of the loop.

But it is not enough to remove the break. You will still have an execution path that will return nothing. You need to return a boolean every time. The code needs to ensure that in all situations it returns something of the same kind.

This method is indicating if you found any user who authenticated, I think you want to inform if you did not authenticate also if you do not find any user with password compatible, then just put a return false at the end of the method went through the whole loop without falling into the if. Thus:

class Program {
    static String[] listaUsuarios = new String[] { "1", "12", "123" };
    public static void main (String[] args) throws java.lang.Exception {
        System.out.println(autentica("123"));
        System.out.println(autentica("456"));
    }
    public static boolean autentica (String usuario) {
        for(int i = 0; i < listaUsuarios.length; i++) {
            if(usuario.equals(listaUsuarios[i])) {
                return true;
            }
        }
        //opcionalmente faz alguma coisa aqui.
        return false;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

There were other errors that prevented the operation. Of course I had to make some adaptations to simplify. Yours would look like this (I think, obviously I didn’t test):

public static boolean autentica (Usuario usuario) {
    for(int i = 0; i < listaUsuarios.length; i++) {
        if(usuario.getSenha().equals(listaUsuarios.get(i).getSenha()) {
            //opcionalmente faz alguma coisa aqui.
            return true;
        }
    }
    //opcionalmente faz alguma coisa aqui.
    return false;
}
  • Thanks, that way you wanted the algorithm, thanks.

  • because you edited? that way does not come out of the loop, the correct form was the one that Voce posted before

  • Are you sure? I’ll do a real test then. If so I’ll go back to what I was. Wait.

  • Voce did not understand the context, this way q vc posted was how I was doing before, but it was wrong because if the 1 user of the list has the password equal to the typed, will return true, however the loop continues, and then will return false, or need the break, so I asked how to use it, and Oce solved it that way.

  • No you don’t understand. If he returns true, the loop ends. Read the first paragraph of the answer. Did you test this way? There is no way it will return true and then return false. Either he makes one or he makes another.

  • then I don’t need to put Else? Else Return false

  • understood now, if it does not return true by default it will be false neh?

  • If you use the else, will not do what you want, because there at the first password check the method will end up saying that not authenticated but without checking the other users. I strongly advise you to run all versions of the code thrashing step-by-step both in authenticating and not authenticating situations. And see what happens for you to understand the logic that I think is what you’re missing. Test including the version with the else that you spoke of. You will better understand the problem.

  • the problem was exactly that then, I used the Else, and that way it worked, thank you

  • You were doing it right, the real problem is that you didn’t return anything when you went through the loop without finding a user with a compatible password. When there is no user with a compatible password it means that you have not authenticated it. You have to inform this and you are not doing it. The only situation that does not authenticate is if it terminates the loop. If it finds a compatible password, it will never end the loop. Because if you have a user with compatible password, you have no reason to look at others. You close the loop and the method too.

  • I understood the logic, I just didn’t know how to write the code.

  • The logic that I initially posed was very complicated and does not serve for this situation, it would serve in another case that I imagined was yours but later I saw that it was not.

Show 7 more comments

1

The return anywhere that is terminates the method (and consequently the loop). The error you should be finding is that the break is dead code, never reached, because the method will always return before reaching it. Remove the break that should work.

  • 1

    Actually just remove the breakwill continue to give error. See my answer.

  • if you remove the break will rotate, however I want to use the break

  • @Warlock cannot place code after Return. If at some point the method reaches Return, it returns and the execution ends. Put the return true; within the if and the return false; at the end of the method.

  • If you understand English I suggest this reading: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Browser other questions tagged

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