1

I'm trying to write a method that looks through an array of objects for a certain color, which is also an object.

public Ghost findFirst(Color c){
        for (int i=0;i<ghosts.length;i++) {
            if (ghosts[i].getColor()==c)
            return ghosts[i];
        else
            return null;
            }  
    }

So if the color of a certain ghost matches color c, then return that ghost. However, I'm getting a dead code warning for i++. Whats wrong with my code? Oh also I'm getting a method error saying this function should return a ghost. I thought I am?

2
  • 1
    Try indenting your code properly, then you'll see for yourself. But basically, you're defeating your for loop, if the first color isn't what you're looking for, you are returning null, and not even looking at all the other colors. Commented Oct 16, 2010 at 20:57
  • 1
    As I had answered earlier on, you will need to use equals() when comparing Color objects. Commented Oct 16, 2010 at 21:02

6 Answers 6

7

Because you return from the loop on the first iteration! So "i" never gets incremented. Either remove the else block completely or change the "return null" to "continue".

As a separate point, the == is checking referential equality, not object equality. It seems likely you should be using ".equals" instead

Sign up to request clarification or add additional context in comments.

4 Comments

I've amended my answer. As a point of interest, what is giving you the dead code warning? Your IDE? Which are you using?
Ya I fixed the .equals thing. I'm using eclipse and i++ is highlited yellow with a dead code warning. I was given instructions to return null if nothing was found though. How can I still implement null in my case?
Also when I removed the else block, I'm still getting a must return Ghost object error
fprime, you need to move the return null outside of the loop (see SKip Head's code example below). Basically your logic needs to be this: "I'm going to go through this array to see if I can find the ghost of the color I want. If I get through the entire list, and I still have not returned a ghost, then I should return null." ... Instead, you have "For each ghost in the list, I will return the ghost if it is that color, and null if it isn't." The problem with that is you can only return once, and you're forcing yourself to return right away. (Since you return before the end, i++ never happens).
3

fixed code:

public Ghost findFirst(Color c){
        for (int i=0;i<ghosts.length;i++) {
            if (ghosts[i].getColor().equals(c))
               return ghosts[i];
        }
        return null;
    }

keep in mind that return terminates the function (including the loop obviously). So if you found the right color ghost - you return it (thus ending your search and never reaching the return null; line). If your for loop found nothing - you get to the last line and return null.

1 Comment

Oh ok, I didnt know return ends a loop. I did a messier method in my fix above, using booleans, which probably wasnt neccessary
2

Its because of

else 
  return null;

!

Because of that return-Statement your Loop will only be executed once.

Comments

2
public Ghost findFirst(Color c){
    for (int i=0; i < ghosts.length; i++) {
        if (ghosts[i].getColor().equals(c))
            return ghosts[i];
    }  
    return null;
}

Comments

1

If I 'unroll' your loop, the code does something like:

public Ghost findFirst(Color c){
    if (ghosts[0].getColor()==c)
       return ghosts[0];
    else
       return null;  

    if (ghosts[1].getColor()==c)
       return ghosts[1];
    else
       return null; 

    if (ghosts[2].getColor()==c)
       return ghosts[2];
    else
       return null; 
    //etc...
}

Should be clear from this, that it will never reach the second if, it returns (breaks out of the function) at the first if, true or false.

Comments

0

Your multiple return may be a problem. Sometime it makes it simpler to have one return.

public Ghost findFirst(Color c) {
    Color color = null;
    for (int i=0;i<ghosts.length;i++) {
        if (ghosts[i].getColor().equals(c))
        color = ghosts[i];
    }
    return color;   
}

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.