Solved

Mortgage Calculator amortization

Posted on 2007-03-26
3
240 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
Java Message handling in Service Layer 3 57
Checkbox and ListView in Android Layout 4 56
split string containing \r\n in Java 46 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 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

21 Experts available now in Live!

Get 1:1 Help Now