[RESOLVED] arrayList problem
hi all,
in my code, only one class object which is c1 displays twice but why not c too?
please help me, i don't understand why is it so.... please anyone :cry:
Code:
public static void main(String args[])throws IOException
{
Customer c =new Customer();
Customer c1 =new Customer();
String cname="";
String uname="";
int pn=0;
double bal=0.0;
int anum=1;
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( isr );
ArrayList<Customer> arrayList = new ArrayList<Customer>();
System.out.println( "Enter your name : " );
cname = br.readLine( );
c.setName(cname);
cname=c.getName();
System.out.println( "Enter your user name : " );
uname = br.readLine( );
c.setUserName(uname);
uname=c.getUserName();
System.out.println("Enter your pincode :");
pn = Integer.parseInt(br.readLine( ));
c.setPinCode(pn);
pn=c.getPinCode();
System.out.println("Enter your balance :");
bal = Double.parseDouble(br.readLine( ));
c.setBalance(bal);
bal=c.getBalance();
System.out.println( "Enter your name : " );
cname = br.readLine( );
c1.setName(cname);
cname=c1.getName();
System.out.println( "Enter your user name : " );
uname = br.readLine( );
c1.setUserName(uname);
uname=c1.getUserName();
System.out.println("Enter your pincode :");
pn = Integer.parseInt(br.readLine( ));
c1.setPinCode(pn);
pn=c1.getPinCode();
System.out.println("Enter your balance :");
bal = Double.parseDouble(br.readLine( ));
c1.setBalance(bal);
bal=c1.getBalance();
c = new Customer(anum , cname , pn , uname , bal);
c1 = new Customer(anum , cname , pn , uname , bal);
arrayList.add(c);
arrayList.add(c1);
System.out.println("Account Successfully Created -- The Account Number Assigned Is : " + anum);
System.out.println("size of list"+arrayList.size());
//*********************display************************
for(int i=0; i<arrayList.size(); i++)
{
System.out.println(arrayList.get(i).toString());
}
}
thanks...
Re: [RESOLVED] arrayList problem
You didn't posted the solution !
Here's a bit cleaned version of your code:
Code:
public class Company
{
public static void main(String args[])throws IOException
{
//Declare all variables
String cname="";
String uname="";
int pn=0;
double bal=0.0;
//Create the reader and arraylist
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
ArrayList<Customer> arrayList = new ArrayList<Customer>();
//Get the number of accounts that needs to be created
System.out.println("Enter the number of accounts you want to create:");
int n = Integer.parseInt(br.readLine( ));
//Create the accounts one by one
for(int i=1;i<=n;i++)
{
System.out.println("\n\nEnter new account details\n---------------");
System.out.println( "\nEnter your name : " );
cname = br.readLine( );
System.out.println( "\nEnter your user name : " );
uname = br.readLine( );
System.out.println("\nEnter your pincode :");
pn = Integer.parseInt(br.readLine( ));
System.out.println("\nEnter your balance :");
bal = Double.parseDouble(br.readLine( ));
//Create the customer object with the input values and add it to arraylist
arrayList.add(new Customer(i , cname , pn , uname , bal));
System.out.println("Account Successfully Created -- The Account Number Assigned Is : " + i);
}
System.out.println("Number of accounts:"+arrayList.size());
//*********************display************************
for(int i=0; i<arrayList.size(); i++)
{
System.out.println(arrayList.get(i).toString()); //assuming you have overrided the toString() function
}
}
}
I didn't tested it. Hope it will work. :wave:
Re: [RESOLVED] arrayList problem
Quote:
Originally Posted by
akhileshbc
You didn't posted the solution !
Here's a bit cleaned version of your code:
Code:
public class Company
{
public static void main(String args[])throws IOException
{
//Declare all variables
String cname="";
String uname="";
int pn=0;
double bal=0.0;
//Create the reader and arraylist
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
ArrayList<Customer> arrayList = new ArrayList<Customer>();
//Get the number of accounts that needs to be created
System.out.println("Enter the number of accounts you want to create:");
int n = Integer.parseInt(br.readLine( ));
//Create the accounts one by one
for(int i=1;i<=n;i++)
{
System.out.println("\n\nEnter new account details\n---------------");
System.out.println( "\nEnter your name : " );
cname = br.readLine( );
System.out.println( "\nEnter your user name : " );
uname = br.readLine( );
System.out.println("\nEnter your pincode :");
pn = Integer.parseInt(br.readLine( ));
System.out.println("\nEnter your balance :");
bal = Double.parseDouble(br.readLine( ));
//Create the customer object with the input values and add it to arraylist
arrayList.add(new Customer(i , cname , pn , uname , bal));
System.out.println("Account Successfully Created -- The Account Number Assigned Is : " + i);
}
System.out.println("Number of accounts:"+arrayList.size());
//*********************display************************
for(int i=0; i<arrayList.size(); i++)
{
System.out.println(arrayList.get(i).toString()); //assuming you have overrided the toString() function
}
}
}
I didn't tested it. Hope it will work. :wave:
thanks :)
this is what i did yesterday.
Code:
import java.util.*;
import java.io.*;
public class Student{
private String name;
public static int num=0;
public void setName(String n)
{
name=n;
}
String getName()
{
return name;
}
public Student(String n)
{
name=n;
}
public Student()
{}
public String toString()
{
return (name);
}
public static void main(String[] args)throws IOException {
String cname;
Student c=new Student();
Student c1=new Student();
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( isr );
System.out.println( "Enter your name : " );
cname = br.readLine( );
c.setName(cname);
cname=c.getName();
Student i1=new Student(cname);
System.out.println( "Enter your name : " );
cname = br.readLine( );
c1.setName(cname);
cname=c1.getName();
Student i2=new Student(cname);
ArrayList<Student> arl=new ArrayList<Student>();
arl.add(i1);
arl.add(i2);
System.out.println("The content of arraylist is: " + arl);
}
}