Solved

# Looping the balance and interest of 3 loans

Posted on 2004-08-20
484 Views
I have seen a few of these mortgage program questions on here, but I can see any that answer my question.  I have most of it figured out, but I am trying to get the 3 mortgages to loop to show the balance and interest of each one separately through the life of the loan.  Please help, I am having a heck of a time with the multidimentional arrays.

/* Modify the mortgage program to display 3 mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%.  Use an array for the different loans.  Display the mortgage payment amount for each loan.  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.  Do not use a graphical user interface.  Insert comments in the program to document the program.

*/

import java.text.DecimalFormat;

class Thursday {  //class name
public static void main(String arguments[]) {

DecimalFormat currency = new DecimalFormat(",000.00");//two decimal format and comma for 1,000's.

double mortgaged = 200000;//Original amount of loan
int loan = 0;

System.out.println("Mortgage Calculator");//Title
System.out.println("                                                                                             ");
System.out.println("Amount Borrowed\t\t" + "Payment \t" + "Interest Rate\t" + "Years\t" ); //Header for schedule

do{  //do/while loop begins
double [][] loans = {
{7, .0535}, {15, .0550}, {30, .0575},}; // Array

double term = loans [loan] [0];//Number of months for mortgage
double interestRate =loans [loan] [1];
double months = term*12;//total months for the loan
double monthlyRate = interestRate/12;//multiplier for monthly interest
double payment = mortgaged*monthlyRate/(1-(Math.pow(1/(1+monthlyRate),months)));// Used for payment amount
int paymentNum = 1;
double balance = mortgaged;
double principal = 0;
double newBalance = 0;
double newMonthlyRate =0;
int lineNum=0;
System.out.println("\$" + currency.format(mortgaged) + "\t\t\$" + currency.format(payment) + "\t" + (interestRate*100)+ " %" + "\t\t"+ term);
loan++;

}//end do/while loop
while (loan<=2);

System.out.println("-------------------------------------------------------------------------------------------------");

System.out.println ("\t\t\tBalances and Interest");
System.out.println("---------------------------------------------------------------------------------------------------");
System.out.println("\t\t7Yr, 5.35%\t\t" + "15Yr, 5.5% \t\t" + "30Yr, 5.75%");
System.out.println("Number\t "+"Balance\t"+"Interest\t"+"Balance\t"+"Interest\t"+"Balance\t"+"Interest" );

//  Everything is good to this point.  Below here is where things get messed up.

while(paymentNum<=30){  //This is where I need my loop that displays the 3 mortgage balances and the interest paid for the life of the loans
Pause(3000);
for (int lineNum=1;paymentNum<=360 && lineNum<=30;lineNum++)
{
newMonthlyRate=balance+monthlyRate;
principal=payment-newMonthlyRate;
newBalance=balance-principal;
balance=newBalance;

System.out.println(" " + paymentNum + "\t\$" + currency.format(newBalance[0][0][0][1]) + "\t\$" +currency.format(newMonthlyRate[0][0][0][1]) + "\t\$" + currency.format(newBalance[1][0][1][1]) + "\t\$" + currency.format(newMonthlyRate[1][0][1][1]) + "\t\$" + currency.format(newBalance[2][0][2][1]) + "\t\$" +currency.format(newMonthlyRate[2][0][2][1]));

paymentNum++;}
}

//above this line is where things are messed up

}
static void Pause(long Milliseconds){
try{
}
catch (InterruptedException e){
e.printStackTrace();
}
}

}//end class
0
Question by:DeeDee2309
[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
• 7
• 7

Author Comment

ID: 11857136
My question is...How do I get the balance and interest looped through the end of the  program.  Each loan is \$200,000.00.  One is 7 years at 5.35% interest, one is 15 year at 5.5% interest and the last is 30 years at 5.75% interest.  I left comments in my program where it works before I added the looping.  If you cut the portion out of my program where I had inserted comments where it does and doesn't work, you can see how I am trying to get it set up.
0

LVL 35

Expert Comment

ID: 11858198
Are you getting any errors? At first glance the logic *seems* correct.
0

LVL 35

Expert Comment

ID: 11858207
Cna you try this and tell me if this is what you are looking for?

import java.text.DecimalFormat;

class Thursday {  //class name

public static void main(String arguments[])
{
DecimalFormat currency = new DecimalFormat(",000.00");//two decimal format and comma for 1,000's.
double mortgaged = 200000;//Original amount of loan
int loan = 0;

System.out.println("Mortgage Calculator");//Title
System.out.println("                                                                                             ");
System.out.println("Amount Borrowed\t\t" + "Payment \t" + "Interest Rate\t" + "Years\t" ); //Header for schedule

double [][] loans = {{7, .0535}, {15, .0550}, {30, .0575},}; // Array

double term = loans [loan] [0];//Number of months for mortgage
double interestRate;// =loans [loan] [1];
double months;// = term*12;//total months for the loan
double monthlyRate; // = interestRate/12;//multiplier for monthly interest
double payment;// = mortgaged*monthlyRate/(1-(Math.pow(1/(1+monthlyRate),months)));// Used for payment amount
int paymentNum = 1;
double balance;// = mortgaged;
double principal = 0;
double newBalance = 0;
double newMonthlyRate =0;
int lineNum=0;

do
{  //do/while loop begins

interestRate =loans [loan] [1];
months = term*12;//total months for the loan
monthlyRate = interestRate/12;//multiplier for monthly interest
payment = mortgaged*monthlyRate/(1-(Math.pow(1/(1+monthlyRate),months)));// Used for payment amount
balance = mortgaged;

System.out.println("\$" + currency.format(mortgaged) + "\t\t\$" + currency.format(payment) + "\t" + (interestRate*100)+ " %" + "\t\t"+ term);
loan++;

}//end do/while loop
while (loan<=2);

System.out.println("-------------------------------------------------------------------------------------------------");

System.out.println ("\t\t\tBalances and Interest");
System.out.println("---------------------------------------------------------------------------------------------------");
System.out.println("\t\t7Yr, 5.35%\t\t" + "15Yr, 5.5% \t\t" + "30Yr, 5.75%");
System.out.println("Number\t "+"Balance\t"+"Interest\t"+"Balance\t"+"Interest\t"+"Balance\t"+"Interest" );

//  Everything is good to this point.  Below here is where things get messed up.

while(paymentNum<=30)
{  //This is where I need my loop that displays the 3 mortgage balances and the interest paid for the life of the loans
Pause(3000);
for (lineNum=1;paymentNum<=360 && lineNum<=30;lineNum++)
{
newMonthlyRate=balance+monthlyRate;
principal=payment-newMonthlyRate;
newBalance=balance-principal;
balance=newBalance;

//System.out.println(" " + paymentNum + "\t\$" + currency.format(newBalance[0][0][0][1]) + "\t\$" +currency.format(newMonthlyRate[0][0][0][1]) + "\t\$" + currency.format(newBalance[1][0][1][1]) + "\t\$" + currency.format(newMonthlyRate[1][0][1][1]) + "\t\$" + currency.format(newBalance[2][0][2][1]) + "\t\$" +currency.format(newMonthlyRate[2][0][2][1]));
System.out.println(" " + paymentNum + "\t\$" + currency.format(newBalance) + "\t\$" +currency.format(newMonthlyRate) + "\t\$" + currency.format(newBalance) + "\t\$" + currency.format(newMonthlyRate) + "\t\$" + currency.format(newBalance) + "\t\$" +currency.format(newMonthlyRate));
paymentNum++;
}
}
//above this line is where things are messed up
}

static void Pause(long Milliseconds){
try{
}
catch (InterruptedException e){
e.printStackTrace();
}
}

}//end class
0

Author Comment

ID: 11862398
Well, that is definitely closer than what I had.  I have to try to get up to 360 payments plus monthly interest paid for the 30 year loan, 180 payments plus monthly interest paid for the 15 year loan and 84 payments plus interest paid for the 7 year loan. I see what you did here, but the total for the intest paid is about  \$211,636,880,508,200.62, and they are all based on a 7 years mortgage, how can I scale that down?  I am just trying to rationalize everything.  Thanks so much!!
0

Author Comment

ID: 11862479
Below is an example of what I am trying to get the above program to do.  This one I made last week.  I am trying to get the 3 separate loans to do the same thing through the array.

import java.text.DecimalFormat;

class Sunday
{  //class name
public static void main(String arguments[])
{
DecimalFormat currency = new DecimalFormat(",000.00");//two decimal format
int term = 360; //360 months for total loan 30 * 12
double interestRate = 5.75/100; //5.75% interest rate
double mortgaged = 200000; // \$200,000 Loan
double monthlyRate = (interestRate/12);//divided my one year
double discountFactor =discountFactor=(Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));// factor to use for amortization;
double payment = mortgaged / discountFactor;
int month=0;
int paymentNum=1;

double balance = mortgaged;
double principal=0;
double newBalance=0;

double newMonthlyRate = 0;
double newPrincipal =0;

//output on screen
System.out.println("Mortgage Program");
System.out.println("Based on a \$" + mortgaged + " mortgage.");
System.out.println("At " + interestRate*100 + " interest");
System.out.println("For " + term/12  + " years");
System.out.println("Your total Monthly payment is :\$" +currency.format(payment));

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

while (paymentNum<=360){

Pause(2500);

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

{
newMonthlyRate = balance*monthlyRate;
principal = payment-newMonthlyRate;
newBalance = balance-principal;
balance = newBalance;

System.out.println(" " + paymentNum + "\t\$ " + currency.format(newMonthlyRate) + "\t\$ " + currency.format(newBalance ));

paymentNum++;
}
}

}

static void Pause(long Milliseconds) {

try {
}
catch (InterruptedException e) {
e.printStackTrace();

}

}

}//end class

0

LVL 35

Expert Comment

ID: 11863160
Cam you try this and tell me if this is what you are looking for:

import java.text.DecimalFormat;

class Sunday
{  //class name
public static void main(String arguments[])
{
double [] loans = {.0535, .0550, .0575}; // Array

DecimalFormat currency = new DecimalFormat(",000.00");//two decimal format
int term = 0;// = 360; //360 months for total loan 30 * 12
double interestRate = 0.0;// = 5.75/100; //5.75% interest rate
double mortgaged = 200000; // \$200,000 Loan
double monthlyRate = 0.0;// = (interestRate/12);//divided my one year
double discountFactor = 0.0;// = (Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));// factor to use for amortization;
double payment = 0.0;// = mortgaged / discountFactor;
/*
int month=0;
int paymentNum=1;

double balance = mortgaged;
double principal=0;
double newBalance=0;

double newMonthlyRate = 0;
double newPrincipal =0;
*/
//output on screen
/*
System.out.println("Mortgage Program");
System.out.println("Based on a \$" + mortgaged + " mortgage.");
System.out.println("At " + interestRate*100 + " interest");
System.out.println("For " + term/12  + " years");
System.out.println("Your total Monthly payment is :\$" +currency.format(payment));
*/
System.out.println( "Number \t" + " Interest\t" + "Ending Balance");

for (int i=0; i<3; i++)
{
term = i == 0 ? 84 : (i == 1 ? 150 : 360);
System.out.println("term: " + term);
interestRate = loans[i];
System.out.println("interestRate : " + interestRate);
monthlyRate = interestRate / 12;
System.out.println("monthlyRate : " + monthlyRate);
discountFactor = (Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));
System.out.println("discountFactor : " + discountFactor);
payment = mortgaged / discountFactor;

System.out.println("Mortgage Program");
System.out.println("Based on a \$" + mortgaged + " mortgage.");
System.out.println("At " + interestRate*100 + " interest");
System.out.println("For " + term/12  + " years");
System.out.println("Your total Monthly payment is :\$" + currency.format(payment));

int month=0;
int paymentNum=1;

double balance = mortgaged;
double principal=0;
double newBalance=0;

double newMonthlyRate = 0;
double newPrincipal =0;

while (paymentNum<term)
{
Pause(2000);
for (int lineNumb=1;paymentNum<=term && lineNumb<=30;lineNumb++)
{
newMonthlyRate = balance*monthlyRate;
principal = payment-newMonthlyRate;
newBalance = balance-principal;
balance = newBalance;
System.out.println(" " + paymentNum + "\t\$ " + currency.format(newMonthlyRate) + "\t\$ " + currency.format(newBalance ));
paymentNum++;
}
}      // while
} // for

}

static void Pause(long Milliseconds)
{

try
{
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}//end class

0

Author Comment

ID: 11864595
yes, that is what I am looking for, but it has to be incorporated into the one you had previously helped me with.  The multidimentional array, as in:

double term = loans [loan] [0];//Number of months for mortgage
double interestRate;// =loans [loan] [1];
double months;// = term*12;//total months for the loan

Now, if I can only figure out what you did.

0

LVL 35

Expert Comment

ID: 11864824
All the functionallity you want is inside the for loop where I use the variables you defined. On each looping I reassign the values to the variables using the new interest rate for the year period. Then based on the terms value I iterate.

Try to replace your while loop in the first programme with the whole for loop in my last programme. It should have worked, otherwise try to step through my code and see what I am doing. If you still need help ask again :)
0

Author Comment

ID: 11867243
Wow, that is excellent.  I added the for loop inside of my original array program and got the folloing error.

-----------------------------------------------------------
"C:\j2sdk1.4.2_05\bin\javac.exe" Thursday.java
Thursday.java:35: incompatible types
found   : double[]
required: double
interestRate = loans[i];
^
1 error
Terminated with exit code 1

-------------------------------------------------------------

Below, I have attached my program.  How do I change that to make it run like that program you just showed me?  You don't know how much I appreciate this!!

import java.text.DecimalFormat;

class Thursday {  //class name
public static void main(String arguments[]) {

DecimalFormat currency = new DecimalFormat(",000.00");//two decimal format and comma for 1,000's.

double [][] loans = {
{84, .0535}, {180, .0550}, {360, .0575},}; // Array

double mortgaged = 200000;//Original amount of loan
int term = 0;
double interestRate = 0.0;
double monthlyRate = 0.0;
double discountFactor = 0.0;
double payment = 0.0;

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

for (int i=0; i<3; i++)
{
term = i == 0 ? 84 : (i == 1 ? 180 : 360);
System.out.println("term: " + term);
interestRate = loans[i];
System.out.println("interestRate : " + interestRate* 100);
monthlyRate = interestRate / 12;
System.out.println("monthlyRate : " + monthlyRate);
discountFactor = (Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));
System.out.println("discountFactor : " + discountFactor);
payment = mortgaged / discountFactor;

System.out.println("Mortgage Program");
System.out.println("Based on a \$" + mortgaged + " mortgage.");
System.out.println("At " + interestRate*100 + " interest");
System.out.println("For " + term/12  + " years");
System.out.println("Your total Monthly payment is :\$" + currency.format(payment));

int month=0;
int paymentNum=1;

double balance = mortgaged;
double principal=0;
double newBalance=0;

double newMonthlyRate = 0;
double newPrincipal =0;

while (paymentNum<term)
{
Pause(3000);
for (int lineNumb=1;paymentNum<=term && lineNumb<=30;lineNumb++)
{
newMonthlyRate = balance*monthlyRate;
principal = payment-newMonthlyRate;
newBalance = balance-principal;
balance = newBalance;
System.out.println(" " + paymentNum + "\t\$ " + currency.format(newMonthlyRate) + "\t\$ " + currency.format(newBalance ));
paymentNum++;
}
}     // while
} // for

}

static void Pause(long Milliseconds)
{

try
{
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}//end class
0

LVL 35

Expert Comment

ID: 11867633
Also change your loans array to this:

double [] loans = {.0535, .0550, .0575}; // Array
0

Author Comment

ID: 11871377
Um, that's what I'm trying to say, it has to look like this:

double [][] loans = {
{84, .0535}, {180, .0550}, {360, .0575},}; // Array

That is what is required, then it has to loop.  That loop you made is absolutley fabulous.  That is what I am trying to get to with the above array.
0

LVL 35

Accepted Solution

girionis earned 500 total points
ID: 11872341
Right, this is a complete example, I just tried it, it's running fine:

import java.text.DecimalFormat;

class Thursday {  //class name
public static void main(String arguments[]) {

DecimalFormat currency = new DecimalFormat(",000.00");//two decimal format and comma for 1,000's.

double [][] loans = {
{84, .0535}, {180, .0550}, {360, .0575},}; // Array

double mortgaged = 200000;//Original amount of loan
int term = 0;
double interestRate = 0.0;
double monthlyRate = 0.0;
double discountFactor = 0.0;
double payment = 0.0;

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

//for (int i=0; i<3; i++)
for (int i=0; i<loans.length; i++)
{
//term = i == 0 ? 84 : (i == 1 ? 180 : 360);
term = (int) loans[i][0];
System.out.println("term: " + term);
interestRate = loans[i][1];
System.out.println("interestRate: " + interestRate);
//System.out.println("interestRate : " + interestRate* 100);
monthlyRate = interestRate / 12;
System.out.println("monthlyRate : " + monthlyRate);
discountFactor = (Math.pow((1 + monthlyRate), term) -1) / (monthlyRate * Math.pow((1 + monthlyRate), term));
System.out.println("discountFactor : " + discountFactor);
payment = mortgaged / discountFactor;

System.out.println("Mortgage Program");
System.out.println("Based on a \$" + mortgaged + " mortgage.");
System.out.println("At " + interestRate*100 + " interest");
System.out.println("For " + term/12  + " years");
System.out.println("Your total Monthly payment is :\$" + currency.format(payment));

int month=0;
int paymentNum=1;

double balance = mortgaged;
double principal=0;
double newBalance=0;

double newMonthlyRate = 0;
double newPrincipal =0;

while (paymentNum<term)
{
Pause(3000);
for (int lineNumb=1;paymentNum<=term && lineNumb<=30;lineNumb++)
{
newMonthlyRate = balance*monthlyRate;
principal = payment-newMonthlyRate;
newBalance = balance-principal;
balance = newBalance;
System.out.println(" " + paymentNum + "\t\$ " + currency.format(newMonthlyRate) + "\t\$ " + currency.format(newBalance ));
paymentNum++;
}
}     // while
} // for

}

static void Pause(long Milliseconds)
{

try
{
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}//end class
0

Author Comment

ID: 11877612
PERFECT!!  Now, how do I give you your points?  I cannot thank you enough for your help, if there is any way I can give you more points, please let me know, it was well worth it.
0

LVL 35

Expert Comment

ID: 11878470
Just accept the answer, there should be a button next to each comment, accept the one comment that you feel has helped you the most. As for additional points, it's fine, I am not here for the points :)

If you need any clarification or there is code that you do not undesrtand let me know :)
0

## Featured Post

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done withâ€¦
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
###### Suggested Courses
Course of the Month3 days, 21 hours left to enroll