# Yet another Java Mortgage calculator topic

Page 1 of 1

## 3 Replies - 1596 Views - Last Post: 06 November 2011 - 02:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254579&amp;s=9083df4d7186687f5c5ba37826ffa7f5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Dizzybunny

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 06-November 11

# Yet another Java Mortgage calculator topic

Posted 06 November 2011 - 02:02 PM

Hello everyone.

I apologize for introducing yet another topic regarding the Java Mortgage Calculator program, but I am at a complete loss at this time. I have done extensive research into this topic including, but not limited to, reading about a dozen threads on this site. The problem that I am having is in regard to calculating and displaying the amortization schedule.
```package LoanPayment;
import java.math.*;
import java.util.*;
import java.text.NumberFormat;

/**
*
* @author MPierce
*/
public class LoanPayment {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double loanAmount = 200000.00;

/*Array for the three different Loan Terms */
double [] loanTerm = new double [3];
loanTerm[0] = 84;
loanTerm[1] = 180;
loanTerm[2] = 360;

/*Array for the three different Annual Interest Rates */
double [] interestRate = new double [3];
interestRate[0] = 0.0535;
interestRate[1] = 0.055;
interestRate[2] = 0.0575;

/*Array for the three different Monthly Interest Rates */
double [] monthlyInterestRate = new double [3];
monthlyInterestRate[0] = (interestRate[0] / 12);
monthlyInterestRate[1] = (interestRate[1] / 12);
monthlyInterestRate[2] = (interestRate[2] / 12);

/*Array for the three different loan payments that will be
calculated within the program*/
double [] payment = new double [3];

/*The interest, principal, balance, and new balance variables are going
* to be used to determine the monthly payment amounts for principal
* paid, interest paid, and the new balance on the mortgage
*/
double interest;
double principal;
double balance;
double newBalance;

/*The term variables are used within the calculation to determine monthly
* payment for each loan
*/
double term1;
double term2;
double term3;
double term4;
double term5;

/*The NumberFormat method automatically rounds up and converts currency
* into US standard form
*/
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);

/*Mortgage formula: L = P[i(1 + i)^n]/[(1 + i)^n - 1]
* L = Payment
* P = Principal
* a = annual interest rate
* i = monthly interest rate: (a/100)/12
* n = payment term
*/

/* In order to save space and give the code a cleaner look, I have
* condensed the calculations sections into their respective
* arrays. */

/* Array indices [0]
* The initial payment calculations are made first with a system output
* added to display the monthly payment, interest paid, and payment
* toward the principal
*/

{ term1 = (1 + monthlyInterestRate[0]);
term2 = Math.pow(term1, loanTerm[0]);
term3 = (monthlyInterestRate[0] * term2);
term4 = (loanAmount * term3);
term5 = (term2 - 1);
payment[0] = (term4 / term5);
interest = (monthlyInterestRate[0] * loanAmount);
principal = (payment[0] - interest);
balance = (loanAmount - principal);
newBalance = (loanAmount - principal);

System.out.println("The montly payment for a \$200,000 mortgage "+
"with an interest rate of 5.35% on a 7 year term is "+
currency.format(payment[0]));
System.out.println("The interest paid for the first payment "+
"is "+ currency.format(interest) + " and the principal "+
"paid is " + currency.format(principal));

/*This section contains the loop for calculating the interest,
* principal, and new balance on the mortgage for each month
* for the remainder of the loan's term
*/
for (int month = 2; month <= loanTerm[0]; month++)
{interest = (monthlyInterestRate[0] * newBalance);
principal = (payment[0] - interest);
newBalance = (newBalance - principal);

/*I added blank output lines at the beginning and end of each output
* as a means of separating the entries and making it easier for the
* user to read
*/
System.out.println("");
System.out.println("Month #" + month);
System.out.println("The payment on principal is "+
currency.format(principal));
System.out.println("Interest paid was: " + currency.format(interest));
System.out.println("The new balance on the loan is: " +
currency.format(newBalance));
System.out.println("");
}}

/* Array indices [1]
* The initial payment calculations are made first with a system output
* added to display the monthly payment, interest paid, and payment
* toward the principal
*/
{System.out.println("");
System.out.println("");
System.out.println("----------New Loan----------");
System.out.println("");
System.out.println("");
term1 = (1 + monthlyInterestRate[1]);
term2 = Math.pow(term1, loanTerm[1]);
term3 = (monthlyInterestRate[1] * term2);
term4 = (loanAmount * term3);
term5 = (term2 - 1);
payment[1] = (term4 / term5);
interest = (monthlyInterestRate[1] * loanAmount);
principal = (payment[1] - interest);
balance = (loanAmount - principal);
newBalance = (loanAmount - principal);

System.out.println("The montly payment for a \$200,000 mortgage "+
"with an interest rate of 5.5% on a 15 year term is "+
currency.format(payment[1]));
System.out.println("The interest paid for the first payment "+
"is "+ currency.format(interest) + " and the principal "+
"paid is " + currency.format(principal));

/*This section contains the loop for calculating the interest,
* principal, and new balance on the mortgage for each month
* for the remainder of the loan's term
*/
for (int month = 2; month <= loanTerm[1]; month++)
{interest = (monthlyInterestRate[1] * newBalance);
principal = (payment[1] - interest);
newBalance = (newBalance - principal);

/*I added blank output lines at the beginning and end of each output
* as a means of separating the entries and making it easier for the
* user to read
*/
System.out.println("");
System.out.println("Month #" + month);
System.out.println("The payment on principal is "+
currency.format(principal));
System.out.println("Interest paid was: " + currency.format(interest));
System.out.println("The new balance on the loan is: " +
currency.format(newBalance));
System.out.println("");
}}

/* Array indices [2]
* The initial payment calculations are made first with a system output
* added to display the monthly payment, interest paid, and payment
* toward the principal
*/
{System.out.println("");
System.out.println("");
System.out.println("----------New Loan----------");
System.out.println("");
System.out.println("");
term1 = (1 + monthlyInterestRate[2]);
term2 = Math.pow(term1, loanTerm[2]);
term3 = (monthlyInterestRate[2] * term2);
term4 = (loanAmount * term3);
term5 = (term2 - 1);
payment[2] = (term4 / term5);
interest = (monthlyInterestRate[2] * loanAmount);
principal = (payment[2] - interest);
balance = (loanAmount - principal);
newBalance = (loanAmount - principal);

System.out.println("The montly payment for a \$200,000 mortgage "+
"with an interest rate of 5.75% on a 30 year term is "+
currency.format(payment[2]));
System.out.println("The interest paid for the first payment "+
"is "+ currency.format(interest) + " and the principal "+
"paid is " + currency.format(principal));

/*This section contains the loop for calculating the interest,
* principal, and new balance on the mortgage for each month
* for the remainder of the loan's term
*/
for (int month = 2; month <= loanTerm[2]; month++)
{interest = (monthlyInterestRate[2] * newBalance);
principal = (payment[2] - interest);
newBalance = (newBalance - principal);

/*I added blank output lines at the beginning and end of each output
* as a means of separating the entries and making it easier for the
* user to read
*/
System.out.println("");
System.out.println("Month #" + month);
System.out.println("The payment on principal is "+
currency.format(principal));
System.out.println("Interest paid was: " + currency.format(interest));
System.out.println("The new balance on the loan is: " +
currency.format(newBalance));
System.out.println("");
}}

}
}

```

Just to be clear the code that I have posted is not the current project that I am working on, but a program that I previously wrote. The current project is borrowing the above code for calculations and adding it to a GUI. The problem that I am having is with this particular portion:
```   for (int month = 2; month <= loanTerm[2]; month++)
{interest = (monthlyInterestRate[2] * newBalance);
principal = (payment[2] - interest);
newBalance = (newBalance - principal);
```

For reasons that are beyond my comprehension this code will not work when inserted into the new program. I need to get it to recalculate the interest, principal, and new loan balance, displaying it into the jTextArea each time. I have spent the last 12 hours trying to find a way to make it work to no avail. Any advice or guidance that anyone can offer would be greatly appreciated.

Thank you.

Is This A Good Question/Topic? 0

## Replies To: Yet another Java Mortgage calculator topic

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Yet another Java Mortgage calculator topic

Posted 06 November 2011 - 02:25 PM

Please post the code you want help with - ONLY the code you want help with - and be clear about what the problem is and what you need help with. "This code will not work," is not explanation enough. Post any error messages you are receiving and describe the program's behaviors you desire and those you are getting that are not desirable.

### #3 Dizzybunny

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 06-November 11

## Re: Yet another Java Mortgage calculator topic

Posted 06 November 2011 - 02:42 PM

Sorry I should have made myself more clear as the nature of my problem. How do I get the program to display the results of each iteration of the loop when it calculates?

Example:
```for (int x = 1; x <=20; x++)
System.out.println("Number" + x);
```

This code will print each number in the console window up to 20. What code do I use to get the program to have the program display each output (1,2,3,...) into a jTextArea?

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Yet another Java Mortgage calculator topic

Posted 06 November 2011 - 02:43 PM

You already have arrays of dimension 3 for loanTerm and interestRate
why don't you have double[] term = new double[3];
what are these term1, term2, term3, term4, term5 ? And why 5 ?