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

Thread: Need help fixing very trivial for loop problem

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help fixing very trivial for loop problem

    // conor brennan
    // cb3131
    // 5/27/14
    // this program gives a monthly report of how much the person should be saving/ the balance of their account
     
    import java.util.Scanner;
     
    public class savingsplan2
    {
      public static void main(String args [])
      {
        Scanner kb = new Scanner(System.in);
     
        System.out.println("What is your savings goal in dollars?"); //prompt user
        double goal = kb.nextDouble();
     
        System.out.println("Enter the expected annual rate of return in percent: ");  
          //prompt user
        double monthlyrate = kb.nextDouble() / 1200;
     
        System.out.println("Enter time until goal in years: "); //prompt user
        double time = kb.nextDouble();
     
        double pv = 1 / Math.pow(1 + monthlyrate, time * 12);
        double save = (goal * pv * monthlyrate / ( 1 - pv));
     
        int savedollar = (int)  save; // integer value of how much should be saved    
                                               //per month
        double savecent = (save - savedollar) * 100; // number of cents left over
        int savecent1 = (int)savecent;  // finds integer value of cents left over
        double savetotal = savedollar + (double)savecent1 / 100; // adds integer         
                                                                                       //of dollars and 
                                                                                       // decimal of 
                                                                                      //cents to find 
                                                                                    //monthly payment 
                                                                                    //to nearest cent
        System.out.println();
        System.out.println("Savings goal: " + goal);
        System.out.println("Annual rate of return: " + monthlyrate * 12 + "%");
        System.out.println("monthly rate: " + monthlyrate + "%");
        System.out.println("Time until goal: " + time + " years");
        System.out.println("to reach this goal, you must save $" + savetotal + " per month");
        System.out.println();
        double interest = 0;
        double balance = 0;
        double totalinterest = 0;
        double finalinterest = 0;
      for (int j = 1; j <= 3; j++){ // prints header for each year long chart
          System.out.println();
          System.out.println("Savings schedule for year " + j);
          System.out.println();
          System.out.println("month   interest     amount        balance");
          System.out.println();
     
     
     
        for (int i = 1; i <= 12; i++) { // prints values in the chart for 3 years
          double amount = savetotal;
          interest = balance * monthlyrate ; // finds monthly interest
          int realinterest = (int)interest;  // gets rid of numbers after decimal
          double realinterest1 = (interest - realinterest) * 100; // gets value after 
                                                                                   //decimal
          double actualinterest = (int)realinterest1;  // gets rid of numbers after 
                                                                    //decimal
          finalinterest = actualinterest / 100 + realinterest; // finds interest to two 
                                                                              //decimal places
          balance = amount + balance + finalinterest; // accumulates balance
          totalinterest = totalinterest + finalinterest; 
          System.out.print(i);
          System.out.println("        " + finalinterest + "        " + amount + "        " + balance);
     
     
        for (int k = i; k >= 12; k--) { // prints footer at end of each year long 
                                                 //chart
          System.out.println();
          System.out.println("Savings summary for year " + j);
          System.out.println("Total amount saved: " + (12 * amount));
          System.out.println("Total interest earned: " + totalinterest);
          System.out.println("End of year balance: " + balance);
          }
          }
        }
      }
    }

    the program is doing everything I want it to do but one thing. I want it to accumulate the interest earned per year and to print that number. By that I mean that I want the interest for year one to be printed after year one, and then I want the interest to reset to 0. I want the interest earned to be only for one individual year at a time. My program currently accumulates the interest each consecutive year, so by year 3, the printed interest is the accumulation of interest earned in year 1, 2, and 3.
    Last edited by cb3131; May 29th, 2014 at 08:58 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Need help fixing very trivial for loop problem

    Please post the program's output and add some comments to show what you want changed.

    Please fix the indentations and the placement of the }s to show the logic.

    Is this the same problem: http://www.java-forums.org/new-java/...ake-momen.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,307
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: Need help fixing very trivial for loop problem

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

Similar Threads

  1. Need help with fixing this error: ArrayIndexOutOfBoundsException: 1
    By mikuya707 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 13th, 2013, 03:23 PM
  2. Need help fixing
    By brown2292 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2012, 01:53 PM
  3. Just Teaching Myself Java--Trivial Problem
    By hroseman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 13th, 2012, 07:07 PM
  4. Help fixing my first program
    By javadude in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 06:52 PM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 12:20 PM

Tags for this Thread