1

Trying to look through array and want it to return true if name is in the array and false if it isn't.

public boolean ifInArray(String name)
{
    for ( Dog breed: breeds) {
        if (breed.equals(name)) {
            return true;
        } else {
            return false;
        }
    }
}
2
  • 1
    you need to move the return false outside the for loop Commented Nov 13, 2014 at 20:23
  • 1
    Also, this should never return true. breed is of type Dog, and name is of type String, so the two should never be equal. If you're written Dog.equals such that it returns true when given a String (for instance, if the String represents the same breed), then that's incorrect. The contract for equals requires that it's symmetric -- that if a.equals(b) then b.equals(a). Since you have no control over String.equals (and in particular, can never make it return true when given a Dog), you should never have Dog.equals return true when given a String. Commented Nov 13, 2014 at 20:42

5 Answers 5

5

fuction must have all possibilities to return a value. think like if for loop does't execute in some conditon then what happen?. so you have give return value for all possibilities

public boolean ifInArray(String name)
{
    for ( Dog breed: breeds) {
        if (breed.equals(name)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much, that makes perfect sense.
@subash You mentioned the general case of "in some condition," I just thought it'd be useful to mention a specific, concrete situation in which that might happen.
The will not look through the entire array.
@LeffeBrune how can I make it look through the entire array?
@tintanten look at the answer by user invalidentry
3

You are only looking at the first entry in the array 'breeds' and immediately returning a true/false value. You need to iterate over every element and only return true if you find a match. Otherwise, return false.

public boolean ifInArray(String name)
{
    for ( Dog breed: breeds) {
        if (breed.equals(name))
            return true;
    }
    return false;
}

4 Comments

How can I make it iterate through the entire array list?
Originally your code would either return true or false after the first comparison. You only want to return true if the breed matches your input string. So you want to remove the 'else' part of your if/else block and only return false if you went through every entry and never found a match.
Thank you! I have done this but now whenever I search it always returns false even when I know it is in the array list
Assuming you are using an IDE like eclipse, I would add a breakpoint at this function and step through it. By doing so you can examine each element of the array as it iterates through it and determine why it is not matching as you expect.
2

Remove the else block and place a return false after the loop ends. If it finds a value, it will return true from inside the loop. If it finds nothing, it will return false after the loop finishes.

Your current implementation will return false if the first breed does not match, rather than checking against all breeds.

Comments

0

You can store the boolean in a variable and just return the variable.

public boolean ifInArray(String name)
{
    Boolean result = null;
    for ( Dog breed: breeds ) {
        if (breed.equals(name)) {
            result = true;
        } else {
            result = false;
        }
    }

    return result;
}

Comments

0

your have returns inside if, so its not accessible to the method directly. there has to be a default if, especially since you have if inside for loop. Consider the case where the string comes as empty, the method will have nothing to return.

write your method as this:

public boolean ifInArray(String name)
{
for ( Dog breed: breeds) {
    if (breed.equals(name)) {
        return true;
    } else {
        return false;
    }
}
return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.