I'm trying to return all of the 'players and their goals' in a sports team using the following:
public String printPlayers(){
for (Player player : this.players){
return player.toString();
}
}
Netbeans says there is no return statement, which I presume is because it is within the for-each loop. But if I place it outside it will only return one item. Here is the test code:
Team barcelona = new Team("FC Barcelona");
Player brian = new Player("Brian");
Player pekka = new Player("Pekka", 39);
barcelona.addPlayer(brian);
barcelona.addPlayer(pekka);
barcelona.addPlayer(new Player("Mikael", 1));
barcelona.printPlayers();
In the Player Object, here is what toString does:
public String toString(){
return ("Player: "+this.name+", goals "+this.goals);
}