[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

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(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
dailydream04
Asked:
dailydream04
  • 3
1 Solution
 
petmagdyCommented:
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
 
petmagdyCommented:
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
 
dailydream04Author Commented:
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
 
petmagdyCommented:
welcome any time :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now