Collection object cannot display output showing some undefined value in java, Arraylist, Collection

When I try to print a collection object, it prints Employee@122392Iie92

. Why does it print this and not the details of the employee list?

My code:

    public class Employee {

    private String name;
    private String designation;
    private int employeeId;
    private int salary;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public int getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }

}

import java.util.ArrayList;

import java.util.Scanner;


public class EmployeeManagement {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList <Employee> lst = new ArrayList <Employee> ();

        System.out.println("Enter the number of employees : ");

        int num = sc.nextInt();

        EmployeeManagement emp = new EmployeeManagement();

        emp.addEmployeeName( num, lst);


    }

    public void addEmployeeName(int num,ArrayList<Employee> lst) {

        Employee em = new Employee();

        for(int i =0; i<num ; i++)
        {
            System.out.println("Enter the employee id : ");
            em.setEmployeeId(sc.nextInt());

            System.out.println("Enter the name of employee : ");
            em.setName(sc.next());

            System.out.println("Enter the designation of employee : ");
            em.setDesignation(sc.next());

            System.out.println("Enter the Salary of employees : ");
            em.setSalary(sc.nextInt());

            lst.add(em);        
        }

        System.out.println(lst);
    }

}

      

+3


source to share


1 answer


It prints using the toString

default method of the class Object

. You must override toString

in the Employee class if you want to show values. The class name and hashcode are currently displayed.

Write a toString method like this in Employee:

 @Override
 public String toString(){
       SubString sb = new SubString();
       sb.append("Name :- ")append(name).append(id);  //all relevant fields
       return sb.toString();
 }

      



Move the new statement inside the loop, otherwise you add and update the same object over and over.

 public void addEmployeeName(int num,ArrayList<Employee> lst) {



    for(int i =0; i<num ; i++)
    {
         Employee em = new Employee();
        System.out.println("Enter the employee id : ");
        em.setEmployeeId(sc.nextInt());

        System.out.println("Enter the name of employee : ");
        em.setName(sc.next());

        System.out.println("Enter the designation of employee : ");
        em.setDesignation(sc.next());

        System.out.println("Enter the Salary of employees : ");
        em.setSalary(sc.nextInt());

        lst.add(em);        
    }

    System.out.println(lst);
}

  }

      

+7


source







All Articles