This site requires JavaScript, please enable it in your browser!
Greenfoot back
BBjorn
BBjorn wrote ...

2016/12/3

Error "mission return statement" please help :p

BBjorn BBjorn

2016/12/3

#
When I try to compile my code, it comes up with the error "missing return statement". Could someone tell me what is wrong here? Help would be appreciated ;) private int listSteak() { List<Steak> steak = getObjectsInRange(1000, Steak.class); for(Steak s: steak) { if (s != null) { s.delete(); s.remove(); } } }
danpost danpost

2016/12/3

#
The method has an 'int' return type instead of 'void'. This means that you cannot just let the method run to the end, but force a return out of the method providing an int value to the calling code. The calling code might be:
int steaks = listSteak();
and the last line of the 'listSteak' method might be:
return steak.size();
This will return the size of the list (number of steak objects that were in range). I cannot say, however, whether you actually wanted some 'int' value returned or not (if not, just change the return type to 'void'). Now, any and all elements in the 'steak' list will contain a Steak object; so, there is no need to check for 'null' values in the list:
You need to login to post a reply.