1

I am trying to get multiple objects to instantiate based on user input. However, I cannot get it to work. There is a player class with a getter and setter method for the users name, but I cannot seem to successfully instantiate the object from my entry. This is just some example code pasted in with what I am trying to do. I don't want to use a database for this, so I just plan to use an array list. The Player is encapsulated inside the for loop and when the for loop exits, the player object created is dead. How can I make this work and allow the in putted name(s) access the Player class right across the player class?

CODE UPDATES: return same value for the getName method in the Player class

Thanks in advance

      ArrayList<Player> players = new ArrayList<Player>();
    int numOfPlayers;

    System.out.println("How many players are in this game?");
    numOfPlayers = input.nextInt();


    for(int i = 0; i < numOfPlayers; i++)
    {
        System.out.print("What is Player " + (i + 1) + " name?");
        String name = input.next();
        Player plr = new Player(); // assuming you have a default constructor
        plr.setName(name);
        players.add(plr);
    }

    for(int i = 0; i < numOfPlayers; i++)
    {
        System.out.println(players.get(i).getName());
    }

Player Class

public class Player
{

    private static String name;

    public void setName(String pName)
    {
        name = pName;
    }

    public String getName()
    {

        return name;
    }
}
2
  • 1
    1. don't use raw types, 2. if players contains string, it doesn't contain Player Commented Mar 11, 2014 at 19:58
  • You should instantiate a Player (assuming that object exist) like Player player = new Player(arg1, arg2..); Commented Mar 11, 2014 at 20:00

4 Answers 4

1

Firstly:

ArrayList<Player> players = new ArrayList<Player>();

Then, in the for loop, you need to declare a new Player object and add that Player to the list. What you're doing now is adding String name to the list, which is not a Player object.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer, it was very helpful. How would I use an element from this arraylist to access the getName method in the Player class though?
players.get(i).getName() where i is the index of the element
Thanks. Strange, it is returning the last input value for all the values. I have updated the code, can you see why?
where is it returning the last input value?
I added in the player class there. It is return the name attribute in the player class
|
1

In the line Player plr = (Player)players.get(i); you are trying to cast a Player object from a String object. Unless your Player class has some type of abstraction to Java's String class you cannot implicitly cast from one to the other. In other words you cannot say Player p = (String) s; or String s = (Player) p; without providing a mechanism to do so. Instead you are going to want to use the new operator to instantiate the Player object.

You didn't post the constructor for your Player object, however if it's an non-defined or empty constructor (meaning it doesn't take any parameters like public Player () {}) you could instantiate the object using the new operator like Player plr = new Player();. Because you probably want to ensure that your player has a name, the better coding practice would be to add a name parameter to your default constructor like public Player(String name){this.setName(name);} which would eliminate the need to manually call setName() everytime you create a new object.

One more thing I would like to point out is that your ArrayList is misleadingly named as it contains "names" (or "playerNames") rather than "players". I think this is likely part of your confusion as you seem to be expecting to get a Playerobject out of it rather than a String. Generics are a good Java construct that you can use to remove some of this ambiguity when dealing with containers. In your case, it may be beneficial to declare ArrayList<Player> players; if Player Objects are what you intend to store there or ArrayList<String> playerNames; if String objects are what you intend to store.

Comments

0

This should work

ArrayList<Player> players = new ArrayList<Player>();
int numOfPlayers;

System.out.println("How many players are in this game?");
numOfPlayers = input.nextInt();


for(int i = 0; i < numOfPlayers; i++)
{
    System.out.print("What is Player " + (i + 1) + " name?");
    String name = input.next();
    Player plr = new Player();
    plr.setName(name);
    players.add(plr);
}

Now all the player objects are accessible from players list.

Player class has a static variable name - since static values are not object dependent and remain same for all the objects of a class - you are not able to get different names for different objects using getName() method.

5 Comments

But when I System.out.println(players.get(0).getName()); & do this for the 2nd entered name, it returns them both as the same name?
why use moniker = pName; in Player class setName method? shouldn't it be this.name = pName;
Also the name property in Player class is static - leading to the same value for all the objects of Player class.
You were absolutely correct. This has cleared up a lot of confusion regarding using static within the class
0

You have never 'created' those objects. All you did was created an array to hold reference to players, but no player were ever created with 'new' keyword and that's the step compiler allocate necessary memory space to hold it's info, such as name.

Actual code on how to do it is not important in my opinion, you might want to refresh your self about Java object life cycles, constructors etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.