Mortgage Calculator

Page 1 of 1

3 Replies - 1951 Views - Last Post: 15 June 2009 - 08:21 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=110046&amp;s=2d76ab86733935358fb75c106e749bd7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jtown2046

Reputation: 0
• Posts: 2
• Joined: 15-June 09

Mortgage Calculator

Posted 15 June 2009 - 12:25 AM

I am trying to modify the mortgage that will allow the user to enter 3 loan terms for the same amount financed. Use an array(s) to store the loans. Then print out a comparison of the payment amount, the total interest paid over the course of the loan, and total of payments.
It will compile but when I run it, it get an error after I enter inerest rate..Please help............

```import java.text.DecimalFormat;
import java.io.*;

import java.text.DecimalFormat;
import java.io.*;

public class Mortgage {

public static void main(String[] args) throws IOException {

//Set the variables
double amount;
int[]  years = new int[3];
String  LoanTerm;
double interest;
double monthly_interest_rate;
double monthly_payment;
double months;
double balance;
double paidInterest;
double paidPrinciple;
int payment=1;

DecimalFormat cur = new DecimalFormat("0.00");

// Output for user data

System.out.println();

int i = 0;
for (i = 0; i < 3; i++)
{
System.out.print("Enter loan term " + (i+1) + " in years:");
years[i] = Integer.parseInt(LoanTerm);
}
System.out.println();

System.out.println();

//Calculation for Monthly Payment

months = (years[i]*12);

monthly_payment = (amount *((interest/100/12) / (1 - Math.pow(1/(1 + interest/100/12), months))));

// The Monthly Payment output
System.out.print("The Monthly Payment is: \$");
System.out.print(cur.format(monthly_payment));
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("Press Enter to Continue");
System.out.println();
System.out.println();

// Pause program and wait user to press enter
System.out.print ("\t\t\t\tAMORTIZATION SCHEDULE");
System.out.println ();
System.out.println ();
System.out.format ("No.\t  Monthly Payment  Interest Paid	Principle Paid\tBalance%n");
System.out.print  ("---\t  ---------------  -------------	--------------\t-------");

// Balance equal to amount
balance=amount;

//Loop for Principal, Interest and balance
for (int num=1; num<=years[i]; num++)
{
//Calculations for Interest paid
paidInterest = (balance * ((interest *.01)/12));

//Calculations for Principle Paid
paidPrinciple = (monthly_payment - paidInterest);

//Calculations for Balance
balance = (balance - paidPrinciple);

//Output for loop
System.out.println();
System.out.format  ("%d",payment++);
System.out.format  ("\t\t" + cur.format(monthly_payment));
System.out.format  ("\t\t" + cur.format(paidPrinciple));
System.out.format  ("\t\t" + cur.format(paidInterest));
System.out.format  ("\t\t" + cur.format(balance));
System.out.print ("");

// Display 20 lines, pauses program and wait user to press enter
if ((num)%20==0)
{

System.out.println();
System.out.println();
System.out.println("Press Enter to Continue");
System.out.println();
System.out.print ("\t\t\t\tAMORTIZATION SCHEDULE");
System.out.println ();
System.out.println ();
System.out.format ("No.\t  Monthly Payment  Interest Paid	Principle Paid\tBalance%n");
System.out.print  ("---\t  ---------------  -------------	--------------\t-------		 ");
}
}
}
}
}
```

This post has been edited by jtown2046: 15 June 2009 - 01:02 PM

Is This A Good Question/Topic? 0

Replies To: Mortgage Calculator

#2 pbl

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

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

Re: Mortgage Calculator

Posted 15 June 2009 - 06:31 PM

```int i = 0;
for (i = 0; i < 3; i++)
{
System.out.print("Enter loan term " + (i+1) + " in years:");
years[i] = Integer.parseInt(LoanTerm);
}

// when you exit de loop here....  i will contain 4

//Calculation for Monthly Payment

months = (years[i]*12);	 <------ and yeras[4] does not exist

```

I guess you need another loop for(i = 0; i < 3; i++) to loop through all your years

#3 jtown2046

Reputation: 0
• Posts: 2
• Joined: 15-June 09

Re: Mortgage Calculator

Posted 15 June 2009 - 08:16 PM

pbl, on 15 Jun, 2009 - 05:31 PM, said:

```int i = 0;
for (i = 0; i < 3; i++)
{
System.out.print("Enter loan term " + (i+1) + " in years:");
years[i] = Integer.parseInt(LoanTerm);
}

// when you exit de loop here....  i will contain 4

//Calculation for Monthly Payment

months = (years[i]*12);	 <------ and yeras[4] does not exist

```

I guess you need another loop for(i = 0; i < 3; i++) to loop through all your years

I don't know how to make it loop through the years

#4 pbl

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

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

Re: Mortgage Calculator

Posted 15 June 2009 - 08:21 PM

```	for(i = 0; i < 3; i++) {
months = years[i] * 12;

.... and all your calculations here

}  <--- end of the for loop

```