missing return statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ILtech
    New Member
    • Oct 2008
    • 6

    missing return statement

    So this is my code. I get the error missing return statement, but I don't see where. Any suggestions would be greatly appreciated.

    (I realize there are probably some other issues in this code as well, but for now, I'm just focused on this one since it's currently preventing me from realizing the others.)

    Code:
    public boolean addHouse ( House a ) {
    		boolean done = false;
    		for (int i = 0; i < collection.length; i++)
    		{
    			if (collection[i] == null && done == false)
    			{
    				collection[i] = a;
    				done = true;
    				return true;
    			}
    			else
    			{
    				done = false;
    				return false;
    			}
    		}
    	}
  • bdan44
    New Member
    • Apr 2007
    • 11

    #2
    try doing something like this
    [code=java]
    public boolean addHouse ( House a ) {
    boolean done = false;
    for (int i = 0; i < collection.leng th; i++)
    {
    if (collection[i] == null && done == false)
    {
    collection[i] = a;
    done = true;
    // REMOVE THIS LINE return true;
    }
    else
    {
    done = false;
    //REMOVE THIS LINE return false;
    }
    }
    //ADD
    return done;
    }[/code]
    Last edited by Nepomuk; Oct 20 '08, 02:55 PM. Reason: Added [CODE] tags

    Comment

    • ILtech
      New Member
      • Oct 2008
      • 6

      #3
      Right, that worked.

      Thank you very much. :]

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Hi bdan44, please make sure to use code tags when posting code in the forums.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          But do you know why your code did not work?
          Reason: if the loop is not executed at all, (in case collection.leng th=0) then the compiler doesn't know what to return. (return-statement after the for-instruction is missing)

          And do you realize that the code suggested from bdan44 does something totally different from yours?
          Your code only runs the loop once (no matter how big the collection is) and then decides on the first element collection[0] to return true or false.

          bdan44 code runs for all elements, always, without break. That means if you have 1000 elements, it always runs 1000 times. Even if the value of for example of the third element is null. That's bad programming, because it runs 999 times for nothing. Important is only the last element that decides if done=true or false.
          All the code of bdan44 could be written without a loop. So the faster version that does the same as bdan44's code is a one-liner:
          Code:
          return (collection[collection.length -1] == null);
          Let me elaborate this with a trace form example above:

          collection[0] == object --> done = false
          collection[1] == object --> done = false
          collection[2] == null --> done = true
          collection[3] == object --> done = false
          collection[4] == object --> done = false
          ...
          collection[999] == object --> done = false


          You see? It doesn't matter if the third element (or any other) was null or not, it just goes on. Only the last element decides. I guess you don't want that it just goes on. I guess it just should loop through all ements until a null was found and then exit immediately. This is the correct algorithm for it:

          Code:
          public boolean addHouse ( House a ) { 
                  for (int i = 0; i < collection.length; i++) 
                  { 
                      if (collection[i] == null) 
                      { 
                          collection[i] = a; 
                          return true; 
                      } 
                   } 
                  return false; 
              }

          Comment

          Working...