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;
}
}
playerscontains string, it doesn't containPlayerPlayer player = new Player(arg1, arg2..);