-
Nov 21st, 2011, 10:34 AM
#1
Thread Starter
Hyperactive Member
[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
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...
-
Nov 21st, 2011, 11:11 AM
#2
Thread Starter
Hyperactive Member
-
Nov 22nd, 2011, 05:56 AM
#3
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.
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Nov 22nd, 2011, 08:56 AM
#4
Thread Starter
Hyperactive Member
Re: [RESOLVED] arrayList problem
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.
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);
}
}
Last edited by Aash; Nov 22nd, 2011 at 09:01 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|