2

Currently, I have 2 objects and I would like to enter a new object.

List<Player> players = new ArrayList<Player>();
players.add(new Player("Eric", 31, true));
players.add(new Player("Juliette", 28, false));

I am looking for a solution on StackOverFlow like below: But, I don't understand my error.

How to add an object into ArrayList in java

Here is my method:

public static void addPlayer(List <Player> players){
    Scanner inputPlayer = new Scanner(System.in);
  
    System.out.print("Enter your player please : ");
    String name = inputPlayer.next();
    Player.add(name);

}

My error is -> Main.java:75: error: cannot find symbol Player.add(name); text

I don't understand my problem??

Player.add(name)

Here is my method called:

case 4 : System.out.println("Option 4 - : ");
         addPlayer(players);
break; 

My Class Player

public class Player {

  public String name;
  public int age; 
  public boolean sex;

  public Player(String name, int age, boolean sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
  }

Thank you in advance for your help.

6
  • 3
    Hi @eric, you are calling Player.add instead of your parameter name: players.add Commented Apr 19, 2021 at 14:31
  • Hi @ gtgaxiola, I always have the same problem Main.java:75: error: incompatible types: String cannot be converted to Player players.add(name); ? Commented Apr 19, 2021 at 14:33
  • 5
    Think about what that statement means. You have a list of objects with the type Player. You're trying to add name which is a String, not a Player. So the error makes sense. Maybe you should have a look at a basic Java tutorial to get a feel for OOP? Commented Apr 19, 2021 at 14:34
  • 3
    You need age and sex inputs too player.add(new Player(name, age, sex)); Commented Apr 19, 2021 at 14:35
  • 1
    You have to get from input all the required data for a new player, create a new player object and add that object to the list. Here you are just trying to add a String to to a List of Player Commented Apr 19, 2021 at 14:37

1 Answer 1

1

You need to take more inputs as your Player constructor requires String name, int age and boolean sex. Then you can instantiate a new Player object with new Player(name, age, sex) and add a Player object to your list with players.add(new Player(name, age, sex));.

public static void addPlayer(List <Player> players){
    Scanner inputPlayer = new Scanner(System.in);
  
    System.out.print("Enter your player name please : ");
    String name = inputPlayer.next();
    System.out.print("Enter your player age please : ");
    int age = inputPlayer.nextInt();
    System.out.print("Enter your player sex (boolean) please : ");
    boolean sex = inputPlayer.nextBoolean();
    players.add(new Player(name, age, sex));

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

1 Comment

Thank you Mady, change just boolean sex = inputPlayer.nextBoolean();

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.