dailydream04
asked on
how to use method with multi-dimensional array.
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(monthsMonthlyRa te[1][i], monthsMonthlyRate[0][i], mortgage);
// Display the monthly payment
System.out.println("\nMort gage Amount \tTerm \t\tRate \t\tMonthly Payment");
System.out.println("$200,0 00 \t\t" + yearsAnnualRate[0][i] + " years\t" + yearAnnualRate[1][i] + "%\t\t" + s);
System.out.println("\nMont h # \tInterest for the Month \t\tLoan Balance");
// Call the method
interestBalanceList(mortga ge, monthsMonthlyRate[1][i],mo nthsMonthl yRate[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.getCurrencyIn stance(Loc ale.US);
String str = n.format(monthlyInterest);
// Formate the loanBalance into U.S. currency
NumberFormat num = NumberFormat.getCurrencyIn stance(Loc ale.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.
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
for(int i = 0; i < 3; i++){
//Call the method
double payment = calPayment(monthsMonthlyRa
// Display the monthly payment
System.out.println("\nMort
System.out.println("$200,0
System.out.println("\nMont
// Call the method
interestBalanceList(mortga
}
}
// 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
// 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.getCurrencyIn
String str = n.format(monthlyInterest);
// Formate the loanBalance into U.S. currency
NumberFormat num = NumberFormat.getCurrencyIn
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
:)
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.
:)
welcome any time :)
>> double monthsMonthlyRate = {{yearsAnnualRate[0][0]*12
should be:
double[][] monthsMonthlyRate = {{yearsAnnualRate[0][0]*12