[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Mortgage Armortization Calculator

Posted on 2007-10-15
10
Medium Priority
?
614 Views
Last Modified: 2013-11-23
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);
            }
      }
      }                                                                                                          
0
Comment
Question by:roscoo
  • 6
  • 3
10 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 20083182
Do you want the LoanAmt to go down whenever a payment is made?
0
 
LVL 92

Expert Comment

by:objects
ID: 20083186
thats cause u don't update your vars
0
 
LVL 92

Expert Comment

by:objects
ID: 20083189
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);
            }
      }
      }                                                  
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:roscoo
ID: 20090181
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
0
 
LVL 92

Expert Comment

by:objects
ID: 20090205
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();
0
 

Author Comment

by:roscoo
ID: 20090293
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"

0
 
LVL 92

Expert Comment

by:objects
ID: 20090331
put it inside the loop
0
 
LVL 92

Expert Comment

by:objects
ID: 20090344
           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();
            }
 
0
 

Author Comment

by:roscoo
ID: 20090353
Now I get an error stating that "unreported exceptions java.io.IOException; must be caught or declared to be thrown"
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20090389
>       public static void main (String[] args)

change that to:

      public static void main (String[] args) throws IOException
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question