Results 1 to 7 of 7
  1. #1
    cb3131 is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Default Need help fixing very trivial definite loop problem - please help. Will take a momen

    Java Code:
    // 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 fake
    {
      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);
          }
          }
        }
      }
    }
    Last edited by cb3131; 05-28-2014 at 11:29 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Need help fixing very trivial definite loop problem - please help. Will take a m

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Need help fixing very trivial definite loop problem - please help. Will take a m

    Please describe the problem - current behaviour, and expected behaviour.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Need help fixing very trivial definite loop problem - please help. Will take a m

    The placing of the {} and the lack of indentation of nested code makes the code very hard to read and understand.
    Can you fix it to make it readable?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Need help fixing very trivial definite loop problem - please help. Will take a m

    The two loops with k an i does not really make sense.
    I is <= 12
    K starts with i and loops while k<=12 - so it is only executed when i=12.
    (so you could replace the for loop with if (i==12)).

    So my suggestion is to split up the code into smaller methods that you can check more easily.

    Konrad

  6. #6
    mousumiy is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Need help fixing very trivial definite loop problem - please help. Will take a m

    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);

    You need to change this and replace the for loop with a while loop or just use plain old IF statement

    if(k==12){
    //Then do something
    }

    The check for the for loop is executed only when the value of i is 12.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Need help fixing very trivial definite loop problem - please help. Will take a m

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help fixing infinite loop
    By Muk in forum New To Java
    Replies: 3
    Last Post: 11-25-2012, 11:13 PM
  2. Replies: 14
    Last Post: 11-06-2012, 09:56 AM
  3. Need help finding my mistake/fixing my problem
    By TANKDS in forum New To Java
    Replies: 7
    Last Post: 10-07-2012, 12:42 PM
  4. Another problem that needs fixing : )
    By Mirr0ge in forum New To Java
    Replies: 3
    Last Post: 03-28-2011, 10:23 PM
  5. Replies: 3
    Last Post: 02-27-2011, 12:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •