Solved

# how to use method with multi-dimensional array.

Posted on 2004-11-07
282 Views
Hi expert,

I'm trying to write a multi-dimensional array to calculate and display the monthly payments for \$200,000 mortgage for 7 years at 5.35%, 15 years at 5.5% and 30 years at 5.75%.  It will also display
the loan balance and interest paid for each payment over the term of the loans.  However, I'm getting the following errors:

']' expected
public static double calPayment(double monthsMonthlyRate[1][i], double monthsMonthlyRate[0][i], double mortgage) {
^
')' expected
}
^
2 errors

Here's the program:

import java.math.*;
import java.text.*;
import java.util.*;

public class MortgageMulti {
public static void main(String[] arguments) {

double mortgage = 200000.00;
double [][] yearsAnnualRate = {{7, 15, 30}, {5.35, 5.5, 5.75}};
double monthsMonthlyRate = {{yearsAnnualRate[0][0]*12, yearsAnnualRate[0][1]*12, yearsAnnualRate[0][2]*12}, { yearsAnnualRate[1][0]/1200, yearsAnnualRate[1][1] / 1200, yearsAnnualRate[1][2] / 1200}};

for(int i = 0; i < 3; i++){

//Call the method
double payment = calPayment(monthsMonthlyRate[1][i], monthsMonthlyRate[0][i], mortgage);

// Display the monthly payment
System.out.println("\nMortgage Amount \tTerm \t\tRate \t\tMonthly Payment");
System.out.println("\$200,000 \t\t" + yearsAnnualRate[0][i] + " years\t" + yearAnnualRate[1][i] + "%\t\t" + s);

System.out.println("\nMonth # \tInterest for the Month \t\tLoan Balance");

// Call the method
interestBalanceList(mortgage, monthsMonthlyRate[1][i],monthsMonthlyRate[0][i], payment);
}
}

// Utility method to calculate the monthly mortgage payment
public static double calPayment(double monthsMonthlyRate[1][i], double monthsMonthlyRate[0][i], double mortgage) {

// Calculate the monthlyPayment
double monthlyPayment = (monthsMonthlyRate[1][i] + monthsMonthlyRate[1][i] / (Math.pow((1 + monthsMonthlyRate[1][i]), monthsMonthlyRate[0][i]) - 1)) * mortgage;

return monthlyPayment;
}

// Utility method to list the interest paid and loan balance
public static void interestBalanceList(double mortgage, double monthsMonthlyRate[1][i], double monthsMonthlyRate[0][i], double payment) {

// Use the for loop to display the list
for (int i = 1; i < (monthsMonthlyRate[0][i] + 1); i++) {

// Pause the list
System.out.println("Hit the 'ENTER' key to exit.");                                     try { System.in.read(); } catch (Exception e) {}
// Calculate the monthlyInterest
double monthlyInterest = mortgage * monthsMonthlyRate[1][i];

// Calculate the loanBlance
double loanBalance = mortgage - (payment - monthlyInterest);

// Set the value of mortgageAmount to the value of loanBalance
mortgage = loanBalance;

// Formate the monthlyInterest into U.S. currency
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
String str = n.format(monthlyInterest);

// Formate the loanBalance into U.S. currency
NumberFormat num = NumberFormat.getCurrencyInstance(Locale.US);
String string = num.format(loanBalance);

// Display the month #, interest paid and loan balance
System.out.println("\n " + i + "\t\t " + str + "\t\t\t" + string);

}
}
}

Does anyone know what I'm doing wrong here?  Thanks.
0
Question by:dailydream04

LVL 13

Accepted Solution

Hi dailydream04,
>     public static double calPayment(double monthsMonthlyRate[1][i],   double monthsMonthlyRate[0][i], double mortgage) { ....}

should be:
public static double calPayment(double param1,   double param2, double mortgage)

and inside this function replace each monthsMonthlyRate[1][i] with param1 and double monthsMonthlyRate[0][i] with param2
0

LVL 13

Expert Comment

also:
>>      double monthsMonthlyRate = {{yearsAnnualRate[0][0]*12, yearsAnnualRate[0][1]*12, yearsAnnualRate[0][2]*12}, { yearsAnnualRate[1][0]/1200, yearsAnnualRate[1][1] / 1200, yearsAnnualRate[1][2] / 1200}};

should be:

double[][] monthsMonthlyRate = {{yearsAnnualRate[0][0]*12, yearsAnnualRate[0][1]*12, yearsAnnualRate[0][2]*12}, { yearsAnnualRate[1][0]/1200, yearsAnnualRate[1][1] / 1200, yearsAnnualRate[1][2] / 1200}};

0

LVL 1

Author Comment

Hi petmagdy,

That makes perfect sense.  I don't know why I didn't think about it earlier.
Thanks for correcting double[][] too.  I realized it when I tried to compile it.
Thanks you again for helping me to understand the multi-dimensional arrays.

:)
0

LVL 13

Expert Comment

welcome any time :)
0

## Featured Post

### Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.