wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Calculating a total after a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculating a total after a "for" loop" Watch "Calculating a total after a "for" loop" New topic
Author

Calculating a total after a "for" loop

Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
Friends,
I have a for loop that adds on a monthly interest rate to two different account balances and prints the new balance for each month 0 - 12. (this part works great!) After it goes through the process I am suppose to print the balance of the accounts added together. The problems is when I do this, I get an extra months worth of interest for both accounts in the total.

Here is my "For loop" code with the subsequent print statement:


Any help would be appreciated!
Thanks,
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

you realize you are looping 13 times, right?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
fred rosenberger wrote:you realize you are looping 13 times, right?


Yes, I did realize I was looping 13 times. I needed an initial total; plus, 12 months of interest. Thanks for the reply though.

However, Idid manage to get it fixed. I simply put the initial month 0 in a separate print statement prior to the loop so I only needed 12 iterations of the loop.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

I would say that is the right solution.

You needed to do this:

print the values 13 times
add the interest 12 times.

So having a loop with a counter of 13 with both printing and adding interest is going to get you an extra month of interest. Your loop should only have a counter of 12. Then, depending on exactly how you write it, you could either



or



Either one would get you 12 months of interest, and print the values 13 times. It's actually pretty common to do things this way.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Another way of fixing the existing code would have been to add the monthly interest only for months 1 to 12.

So you would have the calls enclosed in an if statement


Now the existing code would work, but anyway you have found a fix already


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
I prefer the earlier solution with a statement outside the loop.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
@Campbell - I was just trying to modify the existing code here
But yes I agree, a statement outside the for loop, before the loop actually starts is more readable and even the for loop will then work for the purpose it is required to fulfill - calculating interest for 12 months.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
So we agree in the end
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
When did I disagree with you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calculating a total after a "for" loop
 
Similar Threads
Help code problem please
printf problems
Compound Interest on an object
illegal start of an expression
Having trouble understanding an error code i keep getting. Please help!