# Mortgage Calculator Part 3

Page 1 of 1

## 3 Replies - 2737 Views - Last Post: 27 September 2009 - 12:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=127224&amp;s=f781e5fde6cb885187fa0a60b63b1882&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nuprogrammer

Reputation: 0
• Posts: 2
• Joined: 14-September 09

# Mortgage Calculator Part 3

Posted 21 September 2009 - 08:15 PM

This program calculates the payment amount for 3 mortgage loans: 7 year at 5.35% , 15 year at 5.5%, 30 year at 5.75%. Use and array for the different loans. Display the mortgage payment amount for each loan.

```import java.io.*;

public class MortgageCalculator3
{
public static void main(String[] args) throws IOException
{
//declare and construct variables part 1 code
double PrincipalValue = 200000;
double InterestRate = .0575;
double NotePeriod =  360; // NP= 12*30
double MonthlyPayment = 0;

//declares basic payment formula 	PMT=(PV x IR)/(1-(1 + IR)^-NP)
MonthlyPayment = (PrincipalValue * (InterestRate/12)) / (1-Math.pow((1 + InterestRate/12), -NotePeriod));

//display Mortgage Calculator Console Application Part 1
System.out.println("Mortgage Calculator Part 2");
System.out.println("The Principal Value is \$200,000");
System.out.println("The term of the mortgage is 30 years");
System.out.println("The Annual Interst Rate is 5.75%");
System.out.println();

//calculates results
System.out.printf("The Mortgage Monthly Payment is: \$%.2f", MonthlyPayment);
System.out.println("");
System.out.println("Thank you for choosing McBride Financial Services.");
System.out.println();

//declare variables for part 2 code
double LoanBalance = PrincipalValue;
double PrincipalBal = PrincipalValue;
double InterestPaid = 0;
double PrincipalPaid = 0;
double InterestDue = 0;
double PaymentAmt = 0;
int i=1;

//Start loop here
while (LoanBalance > 0.01)
{

//declare formula to calculate LoanBalance and InterestPaid over the term of the loan.
//PrincipalBal = PrincipalBal  - (MonthlyPayment - InterestPaid );
InterestDue = LoanBalance * (InterestRate/12);
PrincipalPaid = MonthlyPayment - InterestDue;
InterestPaid = MonthlyPayment - PrincipalPaid;
LoanBalance = LoanBalance - PrincipalPaid;
PaymentAmt = PrincipalPaid + InterestPaid;

//display Mortgage Calculator Console Application Part 2
System.out.printf(i + ": Loan Balance: \$%.2f", LoanBalance);
//System.out.println("");
//System.out.printf("The Principal Balance is: \$%.2f", PrincipalBal);
//System.out.println("");
System.out.printf("\tInterest Paid: \$%.2f", InterestPaid);
System.out.println("");
System.out.printf("\tMortgage Payment Amount: \$%.2f", PaymentAmt);
System.out.println("");

i++;
//End of loop

if(LoanBalance <= 0.01)
System.out.println("The Mortgage Loan is paid in full.");
}

}

//Start array here
//declare variables for part 3 code
int LoanTerm[];
LoanTerm = new int[3];
LoanTerm [0] = 7;
LoanTerm [1] = 15;
LoanTerm [2] = 30;
InterestRate = new double [3];
InterestRate = .0535;
InterestRate = .0550;
InterestRate = .0575;
int PrincipalValue[] = 200000;

//Iterating arrays
double LoanTerm[] = {7, 15, 30};
double InterestRate[] = {.0535, .0550, .0575};
double MonthlyPayment[] = {0, 0, 0};

//declare formula to calculate payment amount for 3 mortgage loans
for (int i=0; i<3; i++)
{
MonthlyPayment[i] = (PrincipalValue[i] * (InterestRate[i]/12)) / (1-Math.pow((1 + InterestRate[i]/12), -NotePeriod[i]));
System.out.printf("\tYour Payment Amount for 7 year term at 5.35% is: \$%.2f", MonthlyPayment[i]);
System.out.println("");
System.out.printf("\tYour Payment Amount for 15 year term at 5.5% is: \$%.2f", MonthlyPayment[i]);
System.out.println("");
System.out.printf("\tYour Payment Amount for 30 year term at 5.75% is: \$%.2f", MonthlyPayment[i]);
System.out.println("");
}
}

```

Errors I am experiencing are:

illegal start of type
identifier expected in:
```for (int i=0; i<3; i++)
```

Is This A Good Question/Topic? 0

## Replies To: Mortgage Calculator Part 3

### #2 pbl

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

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

## Re: Mortgage Calculator Part 3

Posted 21 September 2009 - 08:27 PM

LoanTerm is defined twice once as a int once as a double

```	int LoanTerm[];
LoanTerm = new int[3];
LoanTerm [0] = 7;
LoanTerm [1] = 15;
LoanTerm [2] = 30;
....
//Iterating arrays
double LoanTerm[] = {7, 15, 30};

```

same problem with PrincipalValue

```	//declare formula to calculate payment amount for 3 mortgage loans
for (int i=0; i<3; i++)
{
MonthlyPayment[i] = (PrincipalValue[i] * (InterestRate[i]/12)) / (1-Math.pow((1 + InterestRate[i]/12), -NotePeriod[i]));

```

is outside the body of a method and NotePeriod[] is not defined

### #3 LynnL

Reputation: 21
• Posts: 109
• Joined: 13-April 09

## Re: Mortgage Calculator Part 3

Posted 22 September 2009 - 08:35 PM

pbl, on 21 Sep, 2009 - 07:27 PM, said:

LoanTerm is defined twice once as a int once as a double

```	int LoanTerm[];
LoanTerm = new int[3];
LoanTerm [0] = 7;
LoanTerm [1] = 15;
LoanTerm [2] = 30;
....
//Iterating arrays
double LoanTerm[] = {7, 15, 30};

```

same problem with PrincipalValue

```	//declare formula to calculate payment amount for 3 mortgage loans
for (int i=0; i<3; i++)
{
MonthlyPayment[i] = (PrincipalValue[i] * (InterestRate[i]/12)) / (1-Math.pow((1 + InterestRate[i]/12), -NotePeriod[i]));

```

is outside the body of a method and NotePeriod[] is not defined

No response ?

### #4 nuprogrammer

Reputation: 0
• Posts: 2
• Joined: 14-September 09

## Re: Mortgage Calculator Part 3

Posted 27 September 2009 - 12:33 PM

I fixed most of the errors and the program compiled successfully but I do not get the ouput that is required from the assignment. Here is my code thus far:

```import java.io.*;

public class MortgageCalculator3
{
public static void main(String[] args) throws IOException
{
for (int i=0; i<3; i++)
//Start arrays here
//declare variables for part 3 code
{
int LoanTerm[];
LoanTerm = new int[3];
LoanTerm [0] = 7;
LoanTerm [1] = 15;
LoanTerm [2] = 30;

double InterestRate[];
InterestRate = new double [3];
InterestRate[0] = .0535;
InterestRate[1] = .0550;
InterestRate[2] = .0575;

int PrincipalValue = 200000;

double MonthlyPayment[];
MonthlyPayment = new double[3];
MonthlyPayment[0] = 0;
MonthlyPayment[1] = 0;
MonthlyPayment[2] = 0;

//declare formula to calculate payment amount for 3 mortgage loans

MonthlyPayment[0] = (PrincipalValue * (InterestRate[0]/12)) / (1-Math.pow((1 + InterestRate[0]/12), -LoanTerm[0]));
MonthlyPayment[1] = (PrincipalValue * (InterestRate[1]/12)) / (1-Math.pow((1 + InterestRate[1]/12), -LoanTerm[1]));
MonthlyPayment[2] = (PrincipalValue * (InterestRate[2]/12)) / (1-Math.pow((1 + InterestRate[2]/12), -LoanTerm[2]));

//display Mortgage Calculator Console Application Part 4
System.out.printf("\tYour Payment Amount for 7 year term at 5.35% is: \$%.2f", + MonthlyPayment[0]);
System.out.println("");
System.out.printf("\tYour Payment Amount for 15 year term at 5.5% is: \$%.2f", + MonthlyPayment[1]);
System.out.println("");
System.out.printf("\tYour Payment Amount for 30 year term at 5.75% is: \$%.2f", + MonthlyPayment[2]);
System.out.println("");
}
}
}

```

pbl, on 21 Sep, 2009 - 07:27 PM, said:

LoanTerm is defined twice once as a int once as a double

```	int LoanTerm[];
LoanTerm = new int[3];
LoanTerm [0] = 7;
LoanTerm [1] = 15;
LoanTerm [2] = 30;
....
//Iterating arrays
double LoanTerm[] = {7, 15, 30};

```

same problem with PrincipalValue

```	//declare formula to calculate payment amount for 3 mortgage loans
for (int i=0; i<3; i++)
{
MonthlyPayment[i] = (PrincipalValue[i] * (InterestRate[i]/12)) / (1-Math.pow((1 + InterestRate[i]/12), -NotePeriod[i]));

```

is outside the body of a method and NotePeriod[] is not defined