Solved

# how to use method with multi-dimensional array.

Posted on 2004-11-07

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.