The program cannot display the value which had assign to properly.The output is some weird texts.Why?

import java.util.ArrayList; 

class Employeee
{

private String name,department,position;
private int idNumber;

public Employeee()
{
name = null;
position = null;
department = null;
idNumber = 0;
}

public Employeee(String n,String p,String d,int i)
{
name = n;
position = p;
department = d;
idNumber = i;
}

public void setName(String n)
{
name = n;
}

public void setPosition(String p)
{
position = p;
}

public void setDepartment(String d)
{
department = d;
}

public void setidNumber(int i)
{
idNumber = i;
}


public String getPosition()
{
return position;
}

public String getDepartment()
{
return department;
}

public String getName()
{
return name;
}

public int getidNumber()
{
return idNumber;
}

 
}



public class EmployeeeTest
{

public static void main(String[] args)
{
Employeee emp1 = new Employeee("Dr.Nor Sabrina","Vice President","Accounting",47899);
Employeee emp2 = new Employeee("En Ahmad","Programmer","IT",39119);
Employeee emp3 = new Employeee("Prof M Izzudin","Manager","Consultation",66666);
Employeee emp4 = new Employeee("Dr N Izzati","CEO","Elect&Etronic",34521);
Employeee emp5 = new Employeee("Dr A Rahman","Manager","PR",12321);


ArrayList ar = new ArrayList();
ar.add(new Employeee("Dr.Nor Sabrina","Vice President","Accounting",47899));
ar.add(emp2);
ar.add(emp3);
ar.add(emp4);
ar.add(emp5);

 for(int i =0;i<ar.size();i++)
 {System.out.println(ar.get(i));}

 
 
 
}

}

Recommended Answers

All 3 Replies

are you getting something to the effect of, "@43df12"? That's because you're trying to print out an employee object. You have to get the employee's name, title, department, or id number and print that, or write a toString method that outputs all of the info.

The problem is my question request me to Print all the element of the listThe list of data which is consist of a table and create with the 5 objects which could be refer to

Employeee emp1 = new Employeee("Dr.Nor Sabrina","Vice President","Accounting",47899);
Employeee emp2 = new Employeee("En Ahmad","Programmer","IT",39119);
Employeee emp3 = new Employeee("Prof M Izzudin","Manager","Consultation",66666);
Employeee emp4 = new Employeee("Dr N Izzati","CEO","Elect&Etronic",34521);
Employeee emp5 = new Employeee("Dr A Rahman","Manager","PR",12321);

Then , i had populate the 5 object with the add() method to the ArrayList object "ar".After that i need to do is to print all the element in the list as requested by the question.

Anyway i had done some modification on the codes to have some feedback from u guys ,IS IT CORRECT OR NOT ?

import java.util.ArrayList; 

class Employeee
{

private String name,department,position;
private int idNumber;

public Employeee()
{
name = null;
position = null;
department = null;
idNumber = 0;
}

public Employeee(String n,String p,String d,int i)
{
name = n;
position = p;
department = d;
idNumber = i;
}

public void setName(String n)
{
name = n;
}

public void setPosition(String p)
{
position = p;
}

public void setDepartment(String d)
{
department = d;
}

public void setidNumber(int i)
{
idNumber = i;
}


public String getPosition()
{
return position;
}

public String getDepartment()
{
return department;
}

public String getName()
{
return name;
}

public int getidNumber()
{
return idNumber;
}

 
}



public class EmployeeeTest
{

public static void main(String[] args)
{
Employeee emp1 = new Employeee("Dr.Nor Sabrina","Vice President","Accounting",47899);
Employeee emp2 = new Employeee("En Ahmad","Programmer","IT",39119);
Employeee emp3 = new Employeee("Prof M Izzudin","Manager","Consultation",66666);
Employeee emp4 = new Employeee("Dr N Izzati","CEO","Elect&Etronic",34521);
Employeee emp5 = new Employeee("Dr A Rahman","Manager","PR",12321);


ArrayList ar = new ArrayList();
ar.add(emp1.getPosition()  + emp1.getDepartment() + emp1.getName() + emp1.getidNumber());
ar.add(emp2.getPosition() + emp2.getDepartment() + emp2.getName() + emp2.getidNumber());
ar.add(emp3.getPosition() + emp3.getDepartment() + emp3.getName() + emp3.getidNumber());
ar.add(emp4.getPosition()  + emp4.getDepartment() + emp4.getName() + emp4.getidNumber());
ar.add(emp5.getPosition()  + emp5.getDepartment() + emp5.getName() + emp5.getidNumber());



 
  System.out.println(ar);
  

 
 
 
}

}

Your second post is the wrong way to do things. Your first was correct, but you didn't understand the advice given to you.

for(int i =0;i<ar.size();i++)
{
   Employee emp = ar.get(i);
   System.out.println(emp.getPosition()  + emp.getDepartment() + emp.getName() + emp.getidNumber());
}

When you do this:

for(int i =0;i<ar.size();i++)
{
   Employee emp = ar.get(i);
   System.out.println(emp);
}

The toString method of the Employee class is called. Since you didn't override the method of the Object class is called, which returns what you used to get.

Read again the second post suggestion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.