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.

Results 1 to 9 of 9

Thread: How to make a team with min. 2 max 5 players with Objects and Arraylist?

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to make a team with min. 2 max 5 players with Objects and Arraylist?

    SOLVED
    Last edited by DinoKing; April 30th, 2022 at 10:02 AM. Reason: SOLVED

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    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.

  3. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    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

  4. #4
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    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

  5. #5
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    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?

  6. #6
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

        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();
        }
     
    }

  7. The Following User Says Thank You to andreaita44 For This Useful Post:

    DinoKing (April 30th, 2022)

  8. #7
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    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;
        }

  9. #8
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    can you send me the complete tournament class?
    what does readUserInput do?
    Last edited by andreaita44; April 24th, 2022 at 05:33 PM.

  10. The Following User Says Thank You to andreaita44 For This Useful Post:

    DinoKing (April 30th, 2022)

  11. #9
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make a team with min. 2 max 5 players with Objects and Arraylist?

    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;
        }

Similar Threads

  1. Searching ArrayList<Objects> for a name?
    By javacloud in forum Loops & Control Statements
    Replies: 5
    Last Post: October 23rd, 2013, 09:43 AM
  2. Ask objects with parameters in Arraylist
    By sabbath in forum Collections and Generics
    Replies: 5
    Last Post: April 7th, 2013, 07:26 AM
  3. Bubblesort of an ArrayList with objects
    By bondage in forum Algorithms & Recursion
    Replies: 9
    Last Post: January 4th, 2012, 11:51 AM
  4. Really need some help with Objects inside an ArrayList
    By ISuckSoBad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2011, 12:33 PM
  5. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM