SOLVED
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
SOLVED
Last edited by DinoKing; April 30th, 2022 at 10:02 AM. Reason: SOLVED
instead of the arraylist I would use an array of maximum size 5, then I would create a variable that indicates how many players there are in the array. Then I would create add and remove methods.
Okay, but how do I link the two together?
So when I have my ArrayList with teams it knows that Team1 has Jens, Julia and Peter has its players
create a method that returns a String-> foreach element in the array add to a temp variable the name of the player -> return the temp variable
I hear what you are saying, but I can't really see it... Might be because I'm a beginner, but even though I use a for each I will just run through the Array but where is the link between team and its players?
ArrayList<Team> teams - Here I have TeamA on Pos0 and TeamB on Pos1
If i make an TeamA[3] array - {Kim, Julia, Peter} where is the link?
public static void main (String []args){ ArrayList<Team> teams = new ArrayList<>(); Player[] PlayersA = new Player[3]; PlayersA[0] = new Player("Kim"); PlayersA[1] = new Player("Julia"); PlayersA[2] = new Player("Peter"); Team teamA = new Team("SOS", PlayersA); teams.add(teamA); System.out.println(teams.get(0).getTeamPlayers()); }
public class Player { private String playerName; public Player (String playerName){ this.playerName = playerName; } @Override public String toString() { return playerName; } }
import java.util.ArrayList; public class Team { private String teamName; private ArrayList<Player> TeamPlayers = new ArrayList<>(); private int teamNumber; private int teamPoint; private int teamRank; public Team(String teamName, Player[] players) { this.teamName = teamName; for(Player p: players){ addPlayer(p); } } public void addPlayer(Player player) { TeamPlayers.add(player); } public String getTeamName(){ return teamName; } public String getTeamPlayers(){ return TeamPlayers.get(0).toString() + " " + TeamPlayers.get(1).toString() + " "+ TeamPlayers.get(2).toString(); } }
DinoKing (April 30th, 2022)
Thank you very much, your reply lead me in a new direction.
- Unfortunately I now have a different problem, maybe you can see the fix to it.
It is repeating my Player Name: twice before I'm able to give in input and by this it is reducing my player list with 1 (as the first will be empte)
public static Tournament input(int numTeams) { Tournament tournament = new Tournament(); for(int i = 0; i < numTeams; i++) { String teamName = readUserInput("Team name: "); Team team = new Team(teamName); tournament.getTeams().add(team); System.out.println("Hvor mange spillere er der på holdet?"); int numPlayersPerTeam = scan.nextInt(); for(int j = 0; j < numPlayersPerTeam; j++) { String playerName = readUserInput("Player name: "); Player player = new Player(playerName); team.getPlayers().add(player); } } return tournament; }
can you send me the complete tournament class?
what does readUserInput do?
Last edited by andreaita44; April 24th, 2022 at 05:33 PM.
DinoKing (April 30th, 2022)
I found a solution to the issue. Thank you very much for your time and help along the way....
int numPlayersPerTeam = Integer.parseInt(readUserInput("Hvor mange spillere er der på holdet?"));
public static Tournament input(int numTeams) { Tournament tournament = new Tournament(); for (int i = 0; i < numTeams; i++) { String teamName = readUserInput("Team name: "); Team team = new Team(teamName); tournament.getTeams().add(team); int numPlayersPerTeam = Integer.parseInt(readUserInput("Hvor mange spillere er der på holdet?")); for (int j = 0; j < numPlayersPerTeam; j++) { String playerName = readUserInput("Player name: "); Player player = new Player(playerName); team.getPlayers().add(player); } } return tournament; }