Results 1 to 16 of 16

Thread: For loop array

  1. #1
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default For loop array

    Hi,

    This is my code

    Java Code:
            for( Owner ownerLoop : owners ) 
            {
                int tempOwnerMoney = ownerLoop.getMoney();
                tempOwnerMoney -= item.cost();
                ownerLoop.withdrawMoney(tempOwnerMoney); 
            }
    The problem is the same tempOwnerMoney gets withdrawn from every owner in that Array. I want it to take it out of every owner depending on what they start with this is just putting them all as the same based on the first one in the array. Hope this makes sence

    Thanks
    Last edited by TheCase; 12-23-2011 at 05:47 PM. Reason: Fixed code

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: For loop array

    I don't understand line 5 - doesn't look like valid syntax!

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    I want it to take it out of every owner depending on what they start with this is
    You need to add an if statement to detect the "depending on" condition to select which account to take it out of.

  4. #4
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    Sorry. Line 5 was meant to be

    Java Code:
    ownerLoop.withdrawMoney(tempOwnerMoney);
    Calling the withdrawMoney method from the Owner class for each of the owners in the loop but instead it just takes the first one and applies the same withdrawal to them all rather than workng out what they all started with then withdrawing of that makes sence

  5. #5
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    Quote Originally Posted by Norm View Post
    You need to add an if statement to detect the "depending on" condition to select which account to take it out of.
    Sorry for the misunderstanding but I want all accounts in the loop to be tkaen out of but currently it takes the same amount in all accounts it doesn't look at what the person currently has so for example

    Withdrawing 500 from each
    person A starts with 500 new total is 0
    Person B starts with 1000 new total is also 0

    but I want person B new total to be 500

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    Withdrawing 500 from each
    Where do you get the amount you want to take out of each account?
    Is the amount the same for all accounts, or does it change for each account?

  7. #7
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    It wont be the same as each account will have a different balance to start with. I thought theses lines would work (ln 3 & 4)

    int tempOwnerMoney = ownerLoop.getMoney();
    tempOwnerMoney -= item.cost();

    so the first one sets up a field to fetch the users current money and then next one takes that away from the output of the cost method in the item class (the cost will always be the same). Seemed logical but that doesn't not work :(

    Thanks

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    Sorry, I don't see where you answered these questions:
    Where do you get the amount you want to take out of each account? Is it item.cost()?
    Is the amount the same for all accounts, or does it change for each account? The same for all. See above.

    What does the withdrawMoney method on line 5 do?
    Last edited by Norm; 12-23-2011 at 06:00 PM.

  9. #9
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    Sorry I'm bad at explaining
    The amount to take out of each account is from the sum tempOwnerMoney -= item.cost(); ln4
    The amount to be taken out will change for each accoutn in the loop

    Hope this clears up

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    The amount to take out of each account is from the sum
    If the original amount was 500 and the item cost was 100, then you want to withdraw 500-100? or 400???
    That does not make sense. Please explain.

    What does the withdrawMoney method on line 5 do?
    Last edited by Norm; 12-23-2011 at 06:03 PM.

  11. #11
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    If amount was 500 and it costs 100 then it shuld withdraw 400.
    The withdrawMoney is a in a different class where the field for money for the owners are stored this is the code for that method

    Java Code:
        public void withdrawMoney(int withdrawMoneyInput)
        {
            money -= withdrawMoneyInput;
        }
    Hope this is clear

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    If amount was 500 and it costs 100 then it shuld withdraw 400.
    Sorry that makes no sense to me.
    If I have 500 and I buy something that costs 100, I would expect to have 400 left.
    If I take away/withdraw 400, I would have 100 left???

  13. #13
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    Yes you are correct sorry oops

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    Now do you see what you need to change in your program to do it that way?

  15. #15
    TheCase is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: For loop array

    Oh waw oops ... no need for that sum at all doh.
    Thanks a lot :)

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: For loop array

    Your welcome.
    Sometimes it helps if you write a comment with the statement describing what that statement does.
    Java Code:
        int tempOwnerMoney = ownerLoop.getMoney();  // Get money in account
        tempOwnerMoney -= item.cost();  // compute new balance = amount - item cost
        ownerLoop.withdrawMoney(tempOwnerMoney); // Withdraw the new balance ???

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  2. Array and Loop Problem
    By javadog in forum New To Java
    Replies: 2
    Last Post: 09-04-2011, 01:38 PM
  3. Array , Loop ?
    By HearT.Hunt3r in forum New To Java
    Replies: 3
    Last Post: 08-16-2011, 12:41 PM
  4. Array or for loop? or both?
    By kedecr in forum New To Java
    Replies: 4
    Last Post: 03-09-2011, 02:24 PM
  5. Loop through Array in JSP
    By Robert_85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-25-2010, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •