Results 1 to 2 of 2
  1. #1
    pekin21 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default While loops - Having problems

    I have a project for school where we are to calculate the monthly payments of a $200,000 at 5.75% over 30 years. I was able to get this part working with my program.

    The next step was to display the monthly loan balance and interest paid for each month over the term of the loan. Using a WHILE loop I'm able to display the loan balance and interest but it displays the same number every month. I do not know how to use the new loan balance in the loop calculation. Any help would greatly be appreaciated. My program is below.

    package mortgagepaymentcalculator;
    import java.text.NumberFormat;
    /**
    *
    * @author Owner
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    //Calculate the monthly mortgage payment of a $200,000 loan over
    // a 30 year team at 5.75%
    int principal = 200000; // total of loan
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    double rate = .0575; // interest rate
    int months = 12; // months in a year
    int payments = 360; // total number of payments
    double percent = rate/12; //To get the interest rate per month
    double interest = Math.pow(1 + percent, payments); //calculates 1 + percent to the n power

    double monthlypayments = principal * (percent * interest)/(interest - 1); //calculates monthly payments


    System.out.println("The monthly payements for a loan of $" + principal);
    System.out.println("at percentage rate of " + rate);
    System.out.println("with payments totalling " + payments);
    System.out.println("would be " + nf.format(monthlypayments));

    double loanbalance = 200000;

    int count = 1; //initializes the counter for the while loop
    while (count <=360) { //starts the while loop

    //double monthlyinterest = 0;
    //double monthlyprincipal = 0;

    double monthlyinterest = (loanbalance * percent); //calculates interest per month
    double monthlyprincipal = (monthlypayments - monthlyinterest); //calculates monthly principal applied to loan
    loanbalance = principal - monthlyprincipal; //calculates latest monthly principal

    System.out.println("The new loan balance for this month will be " + nf.format(loanbalance));
    System.out.println ("The interest paid this month is " + nf.format(monthlyinterest));



    count++;} //increments each cycle by one



    }
    }

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    my geuss is this line


    Java Code:
    loanbalance = principal - monthlyprincipal; //calculates latest monthly principal
    should be something like this
    Java Code:
    loanbalance -= monthlyprincipal;
    Are principal and loanbalance the same thing? That could be a problem also and may cause this not to work.
    Last edited by yellowledbet; 05-16-2011 at 09:14 AM.

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  2. For loops and while loops
    By patz in forum New To Java
    Replies: 1
    Last Post: 03-20-2011, 06:32 PM
  3. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 02:38 AM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 07:59 PM
  5. Some problems with arrays and loops
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:24 PM

Posting Permissions

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