0
I am making a code where is created an Arraylist class "Employee"
public static void main(String[] args) {
int qntFuncionarios;
Scanner sc = new Scanner (System.in);
List <Empregado> list = new ArrayList<Empregado>();
System.out.println("Favor informar a quantidade de funcionarios que queira cadastrar: ");
qntFuncionarios = sc.nextInt();
for (int i=0; i<qntFuncionarios;i++)
{
Empregado empregado = new Empregado();
System.out.println("Informar o id do funcionario: ");
empregado.id = sc.nextInt();
System.out.println("\n Informar o nome do funcionário: ");
empregado.nome = sc.next();
System.out.println("\n Informar o salario do usuário: ");
empregado.salario = sc.nextDouble();
list.add(new Empregado());
}
for (Empregado x : list)
{
System.out.println(x);
}
}
}
However, when I print through for each, I get that output pattern
"classist.Employee@7d4991ad"
by what I’ve been researching, has something to do with the method toString, but I haven’t found a solution yet.
I actually got out of this hashcode, but I came across another problem, it seems that the information entered, ID, name and salary are not within the list,
– Kirk
@Opufpaulistano It’s because you’re doing
list.add(new Empregado())
, ie, is added a newEmpregado
(and with the data not filled in). Instead, you should add what already had the data filled in:list.add(empregado)
– hkotsubo