Results 1 to 16 of 16
Thread: For loop array
- 12-23-2011, 03:21 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
For loop array
Hi,
This is my code
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 senceJava Code:for( Owner ownerLoop : owners ) { int tempOwnerMoney = ownerLoop.getMoney(); tempOwnerMoney -= item.cost(); ownerLoop.withdrawMoney(tempOwnerMoney); }
ThanksLast edited by TheCase; 12-23-2011 at 04:47 PM. Reason: Fixed code
- 12-23-2011, 03:44 PM #2
Re: For loop array
I don't understand line 5 - doesn't look like valid syntax!
- 12-23-2011, 04:26 PM #3
Re: For loop array
You need to add an if statement to detect the "depending on" condition to select which account to take it out of.I want it to take it out of every owner depending on what they start with this is
- 12-23-2011, 04:26 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: For loop array
Sorry. Line 5 was meant to be
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 senceJava Code:ownerLoop.withdrawMoney(tempOwnerMoney);
- 12-23-2011, 04:29 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: For loop array
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
- 12-23-2011, 04:40 PM #6
Re: For loop array
Where do you get the amount you want to take out of each account?Withdrawing 500 from each
Is the amount the same for all accounts, or does it change for each account?
- 12-23-2011, 04:49 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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
- 12-23-2011, 04:58 PM #8
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 05:00 PM.
- 12-23-2011, 04:59 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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
- 12-23-2011, 05:01 PM #10
Re: For loop array
If the original amount was 500 and the item cost was 100, then you want to withdraw 500-100? or 400???The amount to take out of each account is from the sum
That does not make sense. Please explain.
What does the withdrawMoney method on line 5 do?Last edited by Norm; 12-23-2011 at 05:03 PM.
- 12-23-2011, 05:15 PM #11
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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
Hope this is clearJava Code:public void withdrawMoney(int withdrawMoneyInput) { money -= withdrawMoneyInput; }
- 12-23-2011, 05:19 PM #12
Re: For loop array
Sorry that makes no sense to me.If amount was 500 and it costs 100 then it shuld withdraw 400.
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???
- 12-23-2011, 05:23 PM #13
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: For loop array
Yes you are correct sorry oops
- 12-23-2011, 05:25 PM #14
Re: For loop array
Now do you see what you need to change in your program to do it that way?
- 12-23-2011, 05:30 PM #15
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: For loop array
Oh waw oops ... no need for that sum at all doh.
Thanks a lot :)
- 12-23-2011, 05:34 PM #16
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
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
Array and Loop Problem
By javadog in forum New To JavaReplies: 2Last Post: 09-04-2011, 12:38 PM -
Array , Loop ?
By HearT.Hunt3r in forum New To JavaReplies: 3Last Post: 08-16-2011, 11:41 AM -
Array or for loop? or both?
By kedecr in forum New To JavaReplies: 4Last Post: 03-09-2011, 01:24 PM -
Loop through Array in JSP
By Robert_85 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-25-2010, 09:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks