How do i fix errors in my Mortgage Calculator in my Java Class

Posted on 2009-04-28
Last Modified: 2012-05-07
Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:

- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.

import java.text.DecimalFormat;        // class needed to format decimals

public class Mort_Cal            // program name: Mortgage Calculator


    public static void main(String[] arguments)        // start main() function


    // define variables

    double monthlyPayment;    // monthly payment

    double principal;        // loan principal

    double interestRateYears;    // annual interest rate (percentage)

    double interestRateMonths;     // monthly interst rate

    int []termYears={7,15,30};        // length of loan in years

    int termMonths;        // length of loan in months

    int linecount;        // number of lines to be displayed

    double []balance={0.,0.,0.};        // displays current balance

    double []interestPaid={5.35,5.5,5.75};        // displays the interest paid

    double principalPaid=200000;        // displays the principal paid

    double []monthlyInterestPayment;    // holds current interest payment

    double monthlyPrincipalPayment;    // holds current principal payment

    // formats numbers to display only two decimal places

    java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");

    //for loop//

	   for (int i = 0; i < interestPaid; i++)


	//time  and interest rate//

	          System.out.print("\n\t over " + termYears[i] + " years");

	     System.out.print(" with an interest rate of " + interestPaid[i]);


    // hard coded information to display basic loan information

    System.out.println("\n\n\t*** Mortgage Calculator ***\n\n" +

           "\nLoan Amount: \t$" + dec.format(principal) +

        "\nInterest Rate: \t" + interestRateYears +"%" +

        "\nTerm (Years): \t" + termYears);

    // calculate monthly mortgage payment

    monthlyPayment = (principal * interestRateMonths) /

            (1 - Math.pow(1 + interestRateMonths, - termMonths));

    // displays monthly mortgage payment resulting from above calculation

        System.out.println("\n\nBased on the above criteria, "  +

               "your monthly payment will be: " +

                  "$" + dec.format(monthlyPayment));

    // formula(s) to calculate monthly interest and principal payments

        monthlyInterestPayment = (balance * interestRateMonths);

        monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);

    // format column headers for results to be displayed




    // start while loop

    while (termMonths > 0)


    // information to display

    System.out.println(termMonths + "\t\t$" +      dec.format(monthlyPrincipalPayment) +

        "\t\t$" + dec.format(monthlyInterestPayment) +

        "\t\t$" + dec.format(balance));

    // decrement months


    // calculate interest and principal payments

    monthlyInterestPayment = (balance * interestRateMonths);

    monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);

    balance = (balance - monthlyPrincipalPayment);

for(int monthNumber=1;monthNumber<termYears[i]*12;monthNumber++)


balance[i] = (principal-month);

principalPaid= balance[i];

interestPaid[i]=principal * (interest[i]/(12*100));

System.out.println((monthNumber)+ "\t\t\t" + decimalplace.format(balance[i]) + "\t\t\t" + decimalplace.format(interestpaid[i]));

monthNumber ++;

    if(linecount == 20)


    linecount = 0;



    Thread.sleep(3000);        // pause to last three seconds


    catch (InterruptedException e)



    }    // end if




    }    // end else

               }           // end while



Open in new window

Question by:imagekrazy
    LVL 12

    Accepted Solution

    Whats is the issue you are facing?

    Author Comment

    I am getting a  one error message at the end of the file.
    LVL 12

    Expert Comment

    by:Gibu George
    Whats the error message?

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    fix34  challenge 9 70
    What is the compatible hibernate version for spring 4.3.1 10 50
    IT Company 5 56
    allStar challenge 1 27
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    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:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    728 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

    22 Experts available now in Live!

    Get 1:1 Help Now