1

I have a method that returns all the names of people in a LinkedList for a plane.

However even though there is a return statement in the method, I'm still getting told there is a missing return statement.

How can I work around this without putting another return statement in? Why isn't it considered valid? Does putting another return statement in change what is returned?

Any feedback is greatly appreciated.

public String check() {

        for (Person person: passengers)
            {
                return person.getName();
            }    
    }
Mark
  • 65
  • 1
  • 3
  • 13
  • Every code path must `return` somethingy, i.e. if there is no `passenger` in passengers`, even then the block must `return` some value. Do the needful, such that the method is able to take care of this scenario as well :-) – nIcE cOw May 29 '15 at 00:22
  • This will not return "all the names of people", only the first person's name. A function is exited after a return statement. – SU3 May 29 '15 at 00:22
  • @SU3 What would be the correct way to cycle through all the passengers names then if the return exits the loop? – Mark May 29 '15 at 00:26

3 Answers3

6

Because if passengers is empty, the loop will never be entered.

If the loop is never entered, assuming the only return statement is in it, we have a serious problem, don't you think ? It's like if there were no return at all.

You need to add another return statement outside of the loop.

Also note that the return will automatically exit the method, so I don't think this is exactly what you wanted as per this sentence in your question :

I have a method that returns all the names of people in a LinkedList for a plane.

Edit

As per your edit, here how you can return a list containing all names :

return passengers.
          .stream()
          .map(Person::getName)
          .collect(Collectors.toList());

Note that you will need to change the signature of your method to

public List<String> check()
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • But what would I return? would it be return null;? and does placing it outside the loop mean that after running through the passenger list it would just jump to that return? – Mark May 29 '15 at 00:23
  • What would be the correct way to cycle through all the passengers names then if the return exits the loop? – Mark May 29 '15 at 00:25
  • @Mark You would return what you want to return in the case where your list is empty. And no, that doesn't mean that you would jump to that return as if you already returned in the loop, you will exit the loop. – Jean-François Savard May 29 '15 at 00:25
  • @Mark I edited to give you an example of what I think you want. – Jean-François Savard May 29 '15 at 00:30
4

In answer to your question in the comments. You can only return a single object from a function. You could take another container and populate it with the names and return that. For example,

public LinkedList<String> check() {

  LinkedList<String> names = new LinkedList<String>();

  for (Person person: passengers) {
    names.add( person.getName() );
  }

  return names;
}
SU3
  • 5,064
  • 3
  • 35
  • 66
1

What exactly are you trying to accomplish, here?

Currently, check will only ever return the name of the first passenger. Think about how your program flows and what you want it to do.

To answer your question, you need to have an 'escape' for every possible path in your code. Even if a certain block should always catch and return (not by definition, but just by how you think the code should flow), you need to handle the case such that that block doesn't catch and return. This can be done by either fixing the first block so that it really is a catch-all, or by simply returning or throwing an error if the first block doesn't catch.

i.e.

public boolean check() {
   ... 
   if (shouldAlwaysBeTrue) return false;
}

doesn't work because shouldAlwaysBeTrue is not true by definition.

public boolean check() {
    ... 
    if (shouldAlwaysBeTrue) return false;
    return true;
}
Tyler Sebastian
  • 9,067
  • 6
  • 39
  • 62