Here’s my code:
List<Player> l = new List<Player>();
String[] playerNumber = {"Red", "Green", "Blue", "Yellow" ,"Orange",
"Black", "Purple"};
for(int i = 0; i < numberOfPlayers; i++){
Player playerNumber[i] = new Player();
System.out.println(numberOfPlayers);
System.out.println(playerNumber[i]);
l.add(playerNumber[i]);
}
on the 5th line I’m getting the following error in eclipse:
- Duplicate local variable playerNumber
- Debug Current Instruction Pointer
- Type mismatch: cannot convert from Player to Player[]
- Syntax error on token "i", delete this token
- The method add(Player) in the type List<Player> is not applicable for the arguments
(String)
if I can’t name it like that, how could I succesfully name them differently?
thanks in advance =)
What are you actually trying to accomplish here?
playerNumberis an array of Strings, not Players. Can you say what you are trying to do in English? We’ll help you translate it into Java.Edit:
There are two ways you could “name” your players here. First, you could use a map to associate a name with a player:
Or, if you just want your class Player to have an attribute for its name, you can add that to your class:
It’s unclear to me how you want to use these names so I can’t say one is better than the other. In the above, however, it’s probably better to use the constructor to specify the name, as in @Peter Lawrey’s answer.