I have to display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan but my output isn't changing.

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class MortgagePmt {

     public static void main(String[] args)
     {
         int term; //Term in years
         double principal; //Principal amount
         double rate; //Annual interest rate
         double interestPaid ;
         double principalPaid;
         double principalBalance;
         String input;

         input =  JOptionPane.showInputDialog("Enter the Principal Amount: ");
         principal = Double.parseDouble(input);
         input = JOptionPane.showInputDialog("Enter the rate (%): ");
         rate = Double.parseDouble(input);
         input = JOptionPane.showInputDialog("Enter the term (years): ");
         term = Integer.parseInt(input);

         DecimalFormat two = new DecimalFormat("0.00"); //round to two decimal places

         double interest_rate = rate/1200; //Monthly interest rate
         int yearly_term = term * 12; //Term in months
         double monthlypayment = principal * interest_rate / (1.0 - Math.pow(interest_rate+1, -yearly_term));

         System.out.println("\n\n\t Mortgage Calculator\n\n" +
        "Principle Amount: \t$" + principal +
        "\nInterest Rate (%): \t" + rate + "%" +
        "\nTerm (Years): \t" + term);

        System.out.println("\nThe monthly payment will be: $" + two.format(monthlypayment));

        System.out.println("\n\nMonths\t\tMonthly\t\tInterest\tPrincipal\tBalance");
        System.out.println("Remaining\tPayment\t\tPayment\t\tPayment\t\tRemaining");
        System.out.println("_________  \t_________  \t_________  \t_________ \t_________");



            for (int i = 1; i <= 360; i++) {

             interestPaid = principal * interest_rate;
             principalPaid = monthlypayment - interestPaid;
             principalBalance = principal - principalPaid;

             System.out.println(yearly_term + "\t\t" + two.format(monthlypayment) + "\t\t$" + two.format(interestPaid) +
                         "\t\t$" + two.format(principalPaid) +
                         "\t\t$" + two.format(principalBalance));   

             yearly_term--;

            }

           try{

                Thread.sleep(2000);

            }catch(InterruptedException e){

                //do nothing... for now...

            }
         }

}

this is the output

     Mortgage Calculator

Principle Amount:   $200000.0
Interest Rate (%):  2.0%
Term (Years):   30

The monthly payment will be: $739.24


Months      Monthly     Interest    Principal   Balance
Remaining   Payment     Payment     Payment     Remaining
_________   _________   _________   _________   _________
360         739.24      $333.33     $405.91     $199594.09
359        739.24       $333.33     $405.91     $199594.09
358        739.24       $333.33     $405.91     $199594.09
357        739.24       $333.33     $405.91     $199594.09
356        739.24       $333.33     $405.91     $199594.09
355        739.24       $333.33     $405.91     $199594.09
etc.

Recommended Answers

All 9 Replies

After your initial calculation, you are not updating the values. So obviously, they will not change. Please update them as well.

Does that mean I have to initialize? When I initialize them it complains

Ok, manually calculate the value of each in this loop and see what you are getting. Then you will understand.

 for (int i = 1; i <= 360; i++) {
interestPaid = principal * interest_rate;
principalPaid = monthlypayment - interestPaid;
principalBalance = principal - principalPaid;
System.out.println(yearly_term + "\t\t" + two.format(monthlypayment) + "\t\t$" + two.format(interestPaid) +
"\t\t$" + two.format(principalPaid) +
"\t\t$" + two.format(principalBalance));
yearly_term--;
}

I understand. The calculations are not updating. I guess I don't really understand loops that much. Is there something I should add in the loop or is it something I should change?

Ok, lets talk mathematically. principal paid depends on interest paid. principle balance depends on principal paid. So the main thing is to updated interest paid. So what do interest paid depend on? In that line of though think and see what you need to update.

import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class MortgagePmt {

     public static void main(String[] args)
     {
         int term; //Term in years
         double principal=0; //Principal amount
         double rate; //Annual interest rate
         double interestPaid;
         double principalPaid ;
         //double principalBalance;
         int linecount = 0;
         String input;
         double balance;
         balance = principal;

         input =  JOptionPane.showInputDialog("Enter the Principal Amount: ");
         principal = Double.parseDouble(input);
         input = JOptionPane.showInputDialog("Enter the rate (%): ");
         rate = Double.parseDouble(input);
         input = JOptionPane.showInputDialog("Enter the term (years): ");
         term = Integer.parseInt(input);

         DecimalFormat two = new DecimalFormat("0.00"); //round to two decimal places

         double interest_rate = rate/1200; //Monthly interest rate
         int yearly_term = term * 12; //Term in months
         double monthlypayment = principal * interest_rate / (1.0 - Math.pow(interest_rate+1, -yearly_term));



         System.out.println("\n\n\t Mortgage Calculator\n\n" +
        "Principle Amount: \t$" + principal +
        "\nInterest Rate (%): \t" + rate + "%" +
        "\nTerm (Years): \t" + term);

        System.out.println("\nThe monthly payment will be: $" + two.format(monthlypayment));

        System.out.println("\n\nMonths\t\tMonthly\t\tInterest\tPrincipal\tBalance");
        System.out.println("Remaining\tPayment\t\tPayment\t\tPayment\t\tRemaining");
        System.out.println("_________  \t_________  \t_________  \t_________ \t_________");

            for (int i = 1; i <= 360; i++) {

             interestPaid = balance * interest_rate;
             principalPaid = monthlypayment - interestPaid;
             balance = balance - principalPaid;

             if(linecount==20)
             {
                 linecount = 0;
                 try{
                     Thread.sleep(2000);
                 }
                 catch (InterruptedException e)
                 {
                 }
                 }
             else
             {
                 linecount++;
             }

             System.out.println(yearly_term + "\t\t" + two.format(monthlypayment) + "\t\t$" + two.format(interestPaid) +
                         "\t\t$" + two.format(principalPaid) +
                         "\t\t$" + two.format(balance));   

             yearly_term--;

            }


         }

}

ok I set principal = balance I finally got different values. but they are coming out negative and first interest paid is 0

Months      Monthly     Interest    Principal   Balance
Remaining   Payment     Payment     Payment     Remaining
_________   _________   _________   _________   _________
360         739.24      $0.00       $739.24     $-739.24
359         739.24      $-1.23      $740.47     $-1479.71
358         739.24      $-2.47      $741.71     $-2221.42
357         739.24      $-3.70      $742.94     $-2964.36
356         739.24      $-4.94      $744.18     $-3708.54
355         739.24      $-6.18      $745.42     $-4453.96
354         739.24      $-7.42      $746.66     $-5200.62
353         739.24      $-8.67      $747.91     $-5948.52

OH perfect! I just had to change principal = balance to after I asked the user !

Thank you so much! You provided me so much logic to my mistake. Thank you, life saver! So simple and understandable. Thank you so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.