Actually i had been practicing some of the java programs today(trying to learn it myself). I am trying to write a program which accepts as many employee details from the user and displaying it. I wrote the code for the same, but failed. There where no compile errors. I tried to create an ArrayList to place the details got from the user. But not very sure how that be done. Or should that be done by using an object array?
Can anyone help me? Also please do suggest any ways by which i can improve my coding. I’ll be really thankful.
Below given is my code :
public class EmployeeList{
String empname;
String empaddress;
int empage;
public void getEmployeeDetails(){
try{
final BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Name :");
empname = br.readLine();
System.out.println("Address :");
empaddress = br.readLine();
System.out.println("Age :");
empage = Integer.parseInt(br.readLine());
}
catch(final IOException e){
e.printStackTrace();
}
}
public void displayEmployeeDetails(){
System.out.println("Name :" + empname);
System.out.println("Address :" + empaddress);
System.out.println("Age : " + empage);
}
}
SerialisePersonList.java
public class SerialisePersonList{
public static void main(final String[] args){
final EmployeeList emp = new EmployeeList();
System.out.println("Enter 3 person details");
for(int i = 1; i <= 3; i++){
System.out.println("Enter details of person" + i);
final List details = new ArrayList();
details.add(emp.getEmployeeDetails());
}
System.out.println("The entered person Details are as following");
for(int i = 1; i <= 3; i++){
System.out.println("Details of person" + i);
emp.displayEmployeeDetails();
}
}
}
You need to create arraylist before cycle scope and add particular empoloyee in cycle