Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mortgage Calculator amortization

Posted on 2007-03-26
3
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
elfe69 earned 2000 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

721 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