Beginner : Using a loop to print object array
Question
Code :
create an array that can hold two BankAccount objects.
In this array, add the following BankAccount objects to it:
Account Number Name
123456 Sandra Murphy
789465 John Browne
So far ive created an array of object arrays which seems ok
Code :
BankAccount [] bankAccountArray = new BankAccount [2];
BankAccount bankAccount1 = new BankAccount("123456","Sandra Murphy");
BankAccount bankAccount2 = new BankAccount("789456","John Browne");
bankAccountArray[0] = bankAccount1;
bankAccountArray[1] = bankAccount2;
What i need to do now is
Code :
Display the two bank accounts (objects) on the screen. You must use a loop here to display the data. If you can, try placing this code in a method and call the method to display the bank accounts.
When i use the bankAccount1.length it just says cannot find variable length. I have no idea what to do next
Anyone any suggestions?
Thanks in advance.
Re: Beginner : Using a loop to print object array
Quote:
cannot find variable length
The compiler can not find the variable: length in the class: BankAccount.
Where is it defined?
Are you trying to get the length of an array? What array do you want the length of?
Re: Beginner : Using a loop to print object array
Thanks for the quick reply
i was using it in a for loop
for(int i = 0 ; i < bankAccount1.length i ++ ){
}
I basically need it to run the same amount as the amount of objects that are in array bankAccount1 which is twice.
Re: Beginner : Using a loop to print object array
There is no array named: bankAccount1 in the code you posted.
Re: Beginner : Using a loop to print object array
Well then im miles off :)
Code :
BankAccount bankAccount1 = new BankAccount("123456","Sandra Murphy");
Is this not an array with 2 strings stored in it?
Sorry im a complete beginner. Only at this a few weeks.
Re: Beginner : Using a loop to print object array
bankAccount1 in post#5 is not an array. It a variable the refers to an instance of the BankAccount class.
See the tutorial about arrays:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Beginner : Using a loop to print object array