Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Beginner : Using a loop to print object array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner : Using a loop to print object array

    Question

    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

     
            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

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner : Using a loop to print object array

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner : Using a loop to print object array

    There is no array named: bankAccount1 in the code you posted.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner : Using a loop to print object array

    Well then im miles off

    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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)
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner : Using a loop to print object array

    Thanks

Similar Threads

  1. Question about for loop to print the sum of series of fractions
    By _K_ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 08:08 AM
  2. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. [SOLVED] how to print for-loop output to JTextArea
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 13th, 2010, 02:43 PM
  5. Print out object that is in a tree
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2010, 02:25 AM