Results 1 to 7 of 7
  1. #1
    monkeyhead is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Creating new instances in a loop problems

    Hi there,

    Basically if I do this:

    Java Code:
            Player aPlayer = new Player(1,"John"); 
            Player bPlayer = new Player(2,"Peter"); 
            Player  cPlayer = new Player(3,"Dave"); 
    
    
     aTeam.addPlayer(aPlayer);

    The above code adds all the player objects into an arraylist in the team class and works fine.


    However I'm trying to incorporate this within a loop, so that I can use user input for the parameters for the player object. But every time I try and do it all that seems to happen is that it adds the player successfully, then if I try to add an additional player it replaces the previous with the new player, instead of adding it in addition to the previous player.


    Java Code:
                               case '2':
                    {
                        // add new Player to a team
                        Team team = new Team();
                        Player player = new Player();   
    
    //get and setting the team parameters etc all works fine
    
    
    
    
          //Asks the user to enter a team id and stores the input
                            UserInput.print("Enter player id:");
                           pID = UserInput.readInt();
                            
                            //Asks the user to enter a team id and stores the input
                            UserInput.print("Enter player name:");
                            pName = UserInput.readString();
                        
                       
                        player.setPlayerNumber(pID);
                        player.setPlayerName(pName);
                            
                        
                        
                        team.addPlayer(player);
                        
                         team.listTeamPlayers();

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Creating new instances in a loop problems

    Don't create a new Team inside of the loop, just a Player object. The Team object should be created before the loop and the new Player added to that Team inside of the loop.

  3. #3
    monkeyhead is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Creating new instances in a loop problems

    I've tried that but then if add more than one team it just adds and replaces all teams with the new team.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Creating new instances in a loop problems

    Quote Originally Posted by monkeyhead View Post
    I've tried that but then if add more than one team it just adds and replaces all teams with the new team.
    Sorry, I can't fully understand your problem based on this. You may need to show more code and add a paragraph or two explaining what the code is trying to do and exactly the problem that is occurring. Also, you'll want to put some time into being sure that the code is well formatted so that you, your instructor, and we can understand it well.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Creating new instances in a loop problems

    I agree with Fubarable, and that would be better adding static int variable in your team & player class to count the id's.
    Then the team_id = counter; counter++;

  6. #6
    monkeyhead is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Creating new instances in a loop problems

    Quote Originally Posted by Fubarable View Post
    Don't create a new Team inside of the loop, just a Player object. The Team object should be created before the loop and the new Player added to that Team inside of the loop.
    I took this bit of advice and looked a bit deeper in my code and tidied it up. Basically you were correct, it was just how I was doing it was the problem. Many thanks for the advice and guidance towards solving this issue. I was deliberately being vague not intentionally to be awkward but I was trying to gain an understanding of why the things were going wrong in order to help me learn how to resolve problems in the future.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Creating new instances in a loop problems

    You're welcome, and I'm glad that you've got things working now.

Similar Threads

  1. Replies: 6
    Last Post: 09-12-2011, 02:27 PM
  2. Need Help Creating A Loop!
    By chrisnojen117 in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 07:30 PM
  3. Help with creating a while loop!
    By BAD in forum New To Java
    Replies: 1
    Last Post: 07-09-2010, 09:00 PM
  4. Creating new instances of a Match over and over again
    By Che_Is_Alive in forum Advanced Java
    Replies: 2
    Last Post: 11-19-2009, 06:05 PM
  5. Object Reflection: Creating new instances
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •