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

1. for loop assistance

Hello all. I will not try to skip around and just say it flat out. I need help with some homework. I need to write two loops for a program, and right now I am attempting to code a for loop for the program that will calculate the value of an investment after so many years.
I have it all written, and the sytnax is good ( I think, I get no errors) but something in the logic of my code is wrong, because when I change how many iterations my loop has I get the same amount, or zero.
I have been told in learning programming that many times a second opinion can help, so if anyone can lend a hand, it would be most appreciated.

here is the loop I am using

for ( month = 1; month < 59; month++)
{
answer1 = (amount + (amount * 1.05));
}

and here is the code

// This application performs a simple analysis on an growing investment

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class InvestGrowth
{
public static void main( String args[] )
{

DecimalFormat twoDigits = new DecimalFormat( "0.00" );

// Assuming an investment of \$1000 at 5% per month, this
// loop calculates the value of the investment after 5 years
amount =(1000*1.05);

for ( month = 1; month < 59; month++)
{
answer1 = (amount + (amount * 1.05));
}

// Assuming an investment of \$1000 at 5% per month, this
// loop calculates how long it will take until the investment
// reaches \$1 million.

JOptionPane.showMessageDialog(null,
"\$1000 compounding a 5% per month is valued at \$" +
"It takes " + answer2 + " months to reach \$1 million");

System.exit(0);

}  // end main

}  // end class BactGrowth

To explain some things, I used only 59 months and started at 1 because I set amount at 1000 * 1.05 already, for the first months calculation instead of starting at 0 and 60.  Reply With Quote

3. Re: for loop assistance

When you post, please be sure to ask a specific question. Short of reading a question, we are left to read every part of your program, try to determine what you want it to do, and try to determine where you went wrong. Asking a question saves much time.  Reply With Quote

4. Re: for loop assistance

I apologize, I have been at this for a while. Essentially I cannot get my for loop to work correctly and calculate the investment after so many iterations of the for loop, and when I try to change values of the amount of iterations, i get the same answer, or I get zero, so the only conclusion I can come to is that it is not doing all the iterations. My question is where did I go wrong and how can I fix it?  Reply With Quote

5. Re: for loop assistance

Your loop keeps doing the same calculation over and over again. It never uses the result from the previous iteration (answer) in the next iteration. In other words answer is always on the left hand side of your expression in the loop, and never-changing amount is always on the right side of the expression in the loop.

You will want to learn how to
• Walk through your program as if you were the JVM and were doing the calculation to see why your code is not functioning right.
• do this calculation on paper first, then then reproduce those steps in your loop.  Reply With Quote

6. The Following User Says Thank You to curmudgeon For This Useful Post:

Kseidel (September 17th, 2012)