Method with foreach only returns false

4

I have a method that will check if the debt equals zero ( divida == "0" ). If it returns true , otherwise it returns false .

Code:

    for(String divida : dividas){
        return divida == "0";
        System.out.println(divida+"\n"+b);
    }
    return false;

It only returns false , because the first element is non-zero, but the second is equal, and remains false .

What do I do?

    
asked by anonymous 26.07.2015 / 20:05

1 answer

11

You should use equals to compare the contents of the string, == tests the reference.

    for(String divida : dividas){
        return (divida.equals("0"))

Do you want to understand more about comparisons in Java?
Read this question and your answers: #.

26.07.2015 / 20:17