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 4 of 4

Thread: for loop assistance

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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[] )
       {
          double answer1, amount;
          int answer2, month;
     
          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
          answer1 = 0.0;
          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.
     
          answer2 = 0;
     
     
     
          JOptionPane.showMessageDialog(null, 
                "$1000 compounding a 5% per month is valued at $" +
                twoDigits.format(answer1) + "\n" +
                "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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default 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.
    Last edited by curmudgeon; September 17th, 2012 at 09:12 PM.

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

    Kseidel (September 17th, 2012)

Similar Threads

  1. Programming a DFA in java? need assistance
    By vraj11590 in forum Java Theory & Questions
    Replies: 0
    Last Post: February 4th, 2012, 01:53 PM
  2. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM
  3. New to the complmunity, need some assistance please.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 7th, 2011, 07:14 AM
  4. NEED ASSISTANCE W/ JAVA PROGRAM
    By Neddrick in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 29th, 2010, 09:20 PM
  5. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM