views:

77

answers:

6

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?

+7  A: 

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

oxbow_lakes
Oh I see...but how do I work around this?
fprime
I've amended my answer. As a point of interest, what is giving you the dead code warning? Your IDE? Which are you using?
oxbow_lakes
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?
fprime
Also when I removed the else block, I'm still getting a must return Ghost object error
fprime
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).
glowcoder
+2  A: 

Its because of

else 
  return null;

!

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

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

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.

Tom Teman
Oh ok, I didnt know return ends a loop. I did a messier method in my fix above, using booleans, which probably wasnt neccessary
fprime
@Ishtar - you're right. Edited in the change.
Tom Teman
+1  A: 

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.

Ishtar
A: 

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;   
}
fastcodejava