Link to home
Start Free TrialLog in
Avatar of roscoo
roscoo

asked on

Mortgage Armortization Calculator

Ok I am trying to get this code to work. I want an armotized morgage calculator the shows the date, monthly interest, montly payment, and principal for 360 months. When I run this code it just gives me the same number. I am confused. I think the println is incorrect but not sure what to do. I have seen another question posted but the code was too complicated for me to understand. I just want to keep it very simple.

Thanks

import java.lang.Math; // Math library
import java.text.NumberFormat; // Number formatting
import java.io.*;

public class MortgCalc
      {
      public static void main (String[] args)
            {
            /*setting variables*/

            double principal = 200000;
            double interest_rate = 5.75;
            double term = 360;
            double payment;
            double MonthlyPerc;
            double LoanAmt = principal;
            double Monthly_principal;
            double Monthly_interest;

            /*formula*/

            MonthlyPerc = (interest_rate /12 * .01); // converts interest rate into months

            payment = principal * MonthlyPerc / (1 - Math.pow(1 + MonthlyPerc, -term));

            /*Formats the payment*/

            NumberFormat result_formatter;
                             result_formatter = NumberFormat.getCurrencyInstance();
                          String result_output;
                          result_output = result_formatter.format(payment);

            /*loop*/

            for (int i=1; i < 361; i++)
            {
            Monthly_principal = (payment -(LoanAmt * MonthlyPerc));
            Monthly_interest = (LoanAmt * MonthlyPerc);            

            /*Screen Display*/

            System.out.println(Monthly_principal + Monthly_interest + payment-Monthly_principal);
            }
      }
      }                                                                                                          
Avatar of ozo
ozo
Flag of United States of America image

Do you want the LoanAmt to go down whenever a payment is made?
thats cause u don't update your vars
should be more like:

import java.lang.Math; // Math library
import java.text.NumberFormat; // Number formatting
import java.io.*;

public class MortgCalc
      {
      public static void main (String[] args)
            {
            /*setting variables*/

            double principal = 200000;
            double interest_rate = 5.75;
            double term = 360;
            double payment;
            double MonthlyPerc;
            double LoanAmt = principal;
            double Monthly_principal;
            double Monthly_interest;

            /*formula*/

            MonthlyPerc = (interest_rate /12 * .01); // converts interest rate into months

            payment = principal * MonthlyPerc / (1 - Math.pow(1 + MonthlyPerc, -term));

            /*Formats the payment*/

            NumberFormat result_formatter;
                             result_formatter = NumberFormat.getCurrencyInstance();
                          String result_output;
                          result_output = result_formatter.format(payment);

            /*loop*/

             Monthly_interest = (LoanAmt * MonthlyPerc);            
            for (int i=1; i < 361; i++)
            {
            Monthly_interest = LoanAmt * MonthlyPerc;
            Monthly_principal = (payment - Monthly_interest);
            LoanAmt -= Monthly_principal;
            /*Screen Display*/

            System.out.println(i+" "+Monthly_principal+" "+LoanAmt);
            }
      }
      }                                                  
Avatar of roscoo
roscoo

ASKER

Thanks the code worked great. I really appreciate it.

Is there a way to round the numbers and make it pause so that you are able to view sections at a time.

Thanks
To round the numbers use the NumberFormat class to format them as required.

eg.

            System.out.println(i+" "+result_formatter.format(Monthly_principal)+" "+result_formatter.format(LoanAmt));

To pause use something like:

if (i%20==0) System.in.read();
Avatar of roscoo

ASKER

Not sure where to put the pause statement. When I put it within the loop after the print statements I got an error "cannot find symbol" "variable i"

put it inside the loop
           for (int i=1; i < 361; i++)
            {
            Monthly_interest = LoanAmt * MonthlyPerc;
            Monthly_principal = (payment - Monthly_interest);
            LoanAmt -= Monthly_principal;
            /*Screen Display*/

            System.out.println(i+" "+Monthly_principal+" "+LoanAmt);
            if (i%20==0) System.in.read();
            }
 
Avatar of roscoo

ASKER

Now I get an error stating that "unreported exceptions java.io.IOException; must be caught or declared to be thrown"
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial