Solved

Mortgage Calculator amortization

Posted on 2007-03-26
3
238 Views
Last Modified: 2009-12-16
Ok experts!!! You have yet to let me down and this one has me confused. I have a mortgage calculator that calculates the interest rate for each payment based off a set APR and number of months. The mortgage amount is inputted by the user. The problem I am having is that now the APR is not calculating at all and it displays $0.00. This is not correct....could someone please take a look at the code and let me know what I forgot to do. Thanks
Here is the code out of TextPad:
import java.io.*;
import java.text.DecimalFormat;

public class Conlys
{
      public static void main (String[] args)throws IOException
      {


            //Declare Variables
            //String - for user input
            String Loan = "X" ;

            //Int stores whole number value


            int [] intAPR = {0, 2, 3};
            int Termmonths;
            int [] Years = {7, 15, 30};
            int paymentNum = 1;
            int Term = 0;


            //Double - Store values with decimal places
            double Principal = 0;
            double MonthlyInt;
            double Payment = 1;
            double Division;
            double InterestPd = 0;
            double Balance = 0;
            double [] APR = {5.35, 5.5, 5.75};
            double NewMonthlyRate = 0;
            double NewBalance = 0;
            double MonthlyRate= 0;

            DecimalFormat money = new DecimalFormat( "$0.00");
            BufferedReader dataIn = new BufferedReader( new InputStreamReader(System.in)); //Better character reading



            //Print prompts and get input
            System.out.println("\t\t\tDavid Conly's Mortgage Calculator");
            System.out.println("\tPurpose:Calculate the monthly payment based on customer input.\n");

            try //Validation for user input
            {

                  System.out.print( "\n\tEnter mortgage amount-Whole amounts only, no commas or cents:");
                  Loan = dataIn.readLine (); // user input
                  Principal = Integer.parseInt(Loan); // converts string to integer

            }

            catch (NumberFormatException nfe)
            {

                  System.out.println("Okay, this will be wrong. Follow directions next time." ); //response to incorrect format

            }

            if (Principal <= 0 || Principal >= 1000000)
            {

                  System.out.print("\n\tLoan amount must be between 0 and 1000000. Please try again.");

            }



            //Calculations

            //Term Months Calculation
            Termmonths=Years[0]*12;

            //Monthly Interest Calculation
            MonthlyInt=APR[0]/(12*100);

            //Division Calculation
            Division= 1-(Math.pow (1+MonthlyInt,-Termmonths));

            //Payment Calculation
            Payment=Principal*(MonthlyInt/Division);

            //Balance due after payment calculation
            Balance = Principal - Payment;

            //Interest paid this payment
            InterestPd = Payment * APR[0]*.01;

            //output line for APR percentage selected
            System.out.print("\n\nThe first available APR is " + APR[0]+"%");

            //output line for monthly pmt.
            System.out.println("\nThe Monthly Payment is " + (money.format(Payment)));

            //output line for term in years
            System.out.print("The first available Term in years is " + Years[0]);


            //output line for loan balance after pmt.
            System.out.print("\nThe Loan Balance is " + (money.format(Balance)));

            //output line for interest paid this pmt.
            System.out.print("\nThe Interest Paid Is " + (money.format(InterestPd)));


            while (paymentNum<Termmonths)
            {

            System.out.print("\n\tPress enter to see interest paid and balance\n\n");

            System.in.read();

            System.out.println("\nNumber \t" + "Interest \t" + "Ending Balance");

            for (int lineNumb=1;paymentNum<=Termmonths && lineNumb<=30;lineNumb++)
            {

            NewMonthlyRate = Balance*MonthlyRate;
            Principal = Payment - NewMonthlyRate;
            NewBalance = Balance - Principal;
            Balance = NewBalance;
            System.out.println(" " + paymentNum + "\t " +
            money.format(NewMonthlyRate) + "\t\t " +      money.format(NewBalance));
            paymentNum++;

            }

            }





            //Calculations

            //Term Months Calculation
            Termmonths=Years[1]*12;

            //Monthly Interest Calculation
            MonthlyInt=APR[1]/(12*100);

            //Division Calculation
            Division= 1-(Math.pow (1+MonthlyInt,-Termmonths));

            //Payment Calculation
            Payment=Principal*(MonthlyInt/Division);

            //Balance due after payment calculation
            Balance = Principal - Payment;

            //Interest paid this payment
            InterestPd = Payment * APR[1]*.01;

            //output line for APR percentage selected
            System.out.print("\n\nThe second available APR is " + APR[1]+"%");

            //output line for monthly pmt.
            System.out.println("\nThe Monthly Payment is " + (money.format(Payment)));

            //output line for term in years
            System.out.print("The second available Term in years is " + Years[1]);


            //output line for loan balance after pmt.
            System.out.print("\nThe Loan Balance is " + (money.format(Balance)));

            //output line for interest paid this pmt.
            System.out.print("\nThe Interest Paid Is " + (money.format(InterestPd)));

            while (paymentNum<Termmonths)
            {

            System.out.print("\n\tPress enter to see interest paid and balance\n\n");

            System.in.read();

            System.out.println("\nNumber \t" + "Interest \t" + "Ending Balance");

            for (int lineNumb=1;paymentNum<=Termmonths && lineNumb<=30;lineNumb++)
            {

            NewMonthlyRate = Balance*MonthlyRate;
            Principal = Payment - NewMonthlyRate;
            NewBalance = Balance - Principal;
            Balance = NewBalance;
            System.out.println(" " + paymentNum + "\t " +
            money.format(NewMonthlyRate) + "\t\t " + money.format(NewBalance));
            paymentNum++;

            }

            }




            //Calculations

            //Term Months Calculation
            Termmonths=Years[2]*12;

            //Monthly Interest Calculation
            MonthlyInt=APR[2]/(12*100);

            //Division Calculation
            Division= 1-(Math.pow (1+MonthlyInt,-Termmonths));

            //Payment Calculation
            Payment=Principal*(MonthlyInt/Division);

            //Balance due after payment calculation
            Balance = Principal - Payment;

            //Interest paid this payment
            InterestPd = Payment * APR[2]*.01;

            //output line for APR percentage selected
            System.out.print("\n\nThe third available APR is " + APR[2]+"%");

            //output line for monthly pmt.
            System.out.println("\nThe Monthly Payment is " + (money.format(Payment)));

            //output line for term in years
            System.out.print("The third available Term in years is " + Years[2]);


            //output line for loan balance after pmt.
            System.out.print("\nThe Loan Balance is " + (money.format(Balance)));

            //output line for interest paid this pmt.
            System.out.print("\nThe Interest Paid Is " + (money.format(InterestPd)));

            while (paymentNum<Termmonths)
            {

            System.out.print("\n\tPress enter to see interest paid and balance\n\n");

            System.in.read();

            System.out.println("\nNumber \t" + "Interest \t" + "Ending Balance");

            for (int lineNumb=1;paymentNum<=Termmonths && lineNumb<=30;lineNumb++)
            {

            NewMonthlyRate = Balance*MonthlyRate;
            Principal = Payment - NewMonthlyRate;
            NewBalance = Balance - Principal;
            Balance = NewBalance;
            System.out.println(" " + paymentNum + "\t " +
            money.format(NewMonthlyRate) + "\t\t " + money.format(NewBalance));
            paymentNum++;

            }

            }

                  }
}
0
Comment
Question by:drisden1
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
elfe69 earned 500 total points
ID: 18795968
I tried your program, what I see is that NewMonthlyRate is always 0, otherwise it seems OK.
NewMonthlyRate is always 0 because MonthlyRate is initially set to 0 and never changed, and as NewMonthlyRate is set to Balance*MonthlyRate at lines 123, 187 and 250, it will always be 0 too.
0
 

Author Comment

by:drisden1
ID: 18795992
elfe69,
Ok, I see tht too but what or how should I change that? If I blank it out I still get the same thing.
0
 
LVL 11

Assisted Solution

by:elfe69
elfe69 earned 500 total points
ID: 18796104
You should set MonthlyRate to the monthly interest rate either before your first loop (if the interest rate is fixed) or after the beginning of each loop (if the interest rate changes with the time)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now