I am getting an error message in Eclipse:
The type of the expression must be an array type but it resolved to Player.
I have created an object Player. The user through the JOptionPane enters how many players they want. I am trying to store the players names in an array.
public class Project3 {
public static void main(String[] args){
String input = JOptionPane.showInputDialog("Enter the number of players: ");
int numPlayers = Integer.parseInt(input);
Player nameOfPlayers;
for(int i = 0; i < numPlayers; i++){
nameOfPlayers[i] = new Player(JOptionPane.showInputDialog("Enter the number of players: "));
if (input == null || input.equals(" ")) throw new IllegalArgumentException("Must enter valid name!!!");
}
}
Here’s is my Class Player:
public class Player {
private String name;
public Player(String name){
if(name == null || name.equals(" "))
throw new IllegalArgumentException("Must enter a name. ");
this.name = name;
}
public void addWord(Word w){
}
public int getScore(){
}
}
You’re using the old value of
input(from when you were asking for the number of players). You probably want something more like this:Edit: Based on the error message you posted, the problem is that
nameOfPlayersisn’t an array, but you’re treating it like one. TryPlayer[] players = new Player[numPlayers];instead.