Wednesday 3 April 2013

In ArrayList setdata getdata Example in java

//---- You can use this example to understand how to set and get data in java --- //

package aman.sort;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Run {

/**
 * @param args
 */
public static void main(String[] args) {

List dataBean = new ArrayList();
try {
String ch;
do{
DataBean bean = new DataBean();
ch="";
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the User :");
    String name = br.readLine();
    bean.setName(name);
    System.out.println("Enter the Id :");
    int id = (Integer.parseInt(br.readLine()));
    bean.setId(id);
    dataBean.add(bean);
    System.out.println("Press 'y' for input again data... ");
    ch = br.readLine();
}while((ch.equals("y"))||ch.equals("Y"));
}
catch(Exception e) {
System.out.println("Error occured due to input invalid data..."+e);
}
for(Object obj: dataBean) {
DataBean getbean  = (DataBean)obj;
System.out.println("name is :"+getbean.getName()+"Id is:"+getbean.getId());
}

}

}


//=================================================

package aman.sort;

public class DataBean {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

No comments:

Post a Comment