0

I'm trying to send to a ArrayList Strings that come from the user input:

private static void adicionarReserva() {
    Scanner adiciona = new Scanner(System.in);
    System.out.println("Numero da pista: ");
        int nPista = adiciona.nextInt();
    System.out.println("Numero de jogadores: ");
        int nJogadores = adiciona.nextInt();
    System.out.println("Data Inicio: ");
        String data_inicio = adiciona.next();

    Reservas reserva = new Reservas(nPista, data_inicio);
    ArrayList<Jogadores> nome_jogador = new ArrayList(); 

    for (int i=1;i<=nJogadores;i++) {
        System.out.println("Nome Jogador: ");
        String nome = adiciona.next();
        nome_jogador.add(nome);
    }

    reserva.addJogadores(nome_jogador);

}

In my Class called: Reservas, i have this:

public void addJogadores(Jogadores lista_jogadores) {
    this.lista_jogadores.add(lista_jogadores);
}

But i have this error:

nome_jogador.add(nome);

The method add(Jogadores) in the type ArrayList is not applicable for the arguments (String)

And this one:

reserva.addJogadores(nome_jogador);

The method addJogadores(Jogadores) in the type Reservas is not applicable for the arguments (ArrayList)

Any one can help me out?

4 Answers 4

3

It's very meaningful error you're getting.

This is your ArrayList:

ArrayList<Jogadores> nome_jogador = new ArrayList(); 
          ↑

What are you trying to insert to it? a String, but it suppose to have Jogadores in it.

Now look at your addJogadores method signature:

public void addJogadores(Jogadores lista_jogadores)
                         ↑

It accepts Jogadoers object and not an ArrayList.

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

Comments

0

You have to pass an instance of class Jogadores, if Jogadores has a constructor which accepts a String then:

nome_jogador.add(new Jogadores(nome));

Comments

0
ArrayList<Jogadores> nome_jogador = new ArrayList();

You are only allowed to add objects that are an instance of Jogadores and because you are trying to add a String (which is obviously not an instance of Jogadores) it gives you an error.

Comments

0
private static void adicionarReserva() {
    Scanner adiciona = new Scanner(System.in);
    System.out.println("Numero da pista: ");
        int nPista = adiciona.nextInt();
    System.out.println("Numero de jogadores: ");
        int nJogadores = adiciona.nextInt();
    System.out.println("Data Inicio: ");
        String data_inicio = adiciona.next();

    Reservas reserva = new Reservas(nPista, data_inicio);
    ArrayList<Jogadores> nome_jogador = new ArrayList(); 

    for (int i=1;i<=nJogadores;i++) {
        System.out.println("Nome Jogador: ");
        String nome = adiciona.next();
        nome_jogador.add(new Jogadores(nome));
    }

    reserva.addJogadores(nome_jogador);

}

In your Class called: Reservas

public void addJogadores(ArrayList<Jogadores> lista_jogadores) {
    this.lista_jogadores.addAll(lista_jogadores);
}

Comments

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.