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?
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
.
您必须传递类Jogadores
的实例,如果Jogadores
具有接受String
的构造函数,则:
nome_jogador.add(new Jogadores(nome));
ArrayList<Jogadores> nome_jogador = new ArrayList();
仅允许您添加作为Jogadores
实例的对象,并且由于您尝试添加String
(显然不是Jogadores
的实例),因此会产生错误。
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.