Hello:

I am a student and am attempting to write a multidimensional array for a Mortgage Calculator program. We have to display three different mortgage's that display 3 different interest rates and term years that show the calculated mortgage payment amount. I have the following code and am receiving many errors and I can't progress unless I can figure out what I am doing wrong, so if anyone can give me some guidance I would appreciate it. Thanks. ljraven

import java.util.*;


public class MortgageArray
{
public static void main(String[] args) throws Exception
{


double[][] mortgage = new mortgage [3][4];


{   {200000, 7, 5.35, 0.0},
{200000, 15, 5.5, 0.0},
{200000, 30, 5.75, 0.0} };


System.out.println("Mortgage amount is \t" + $);
System.out.println("Term of mortgage is \t" +  "years");
System.out.println("Interest rate is \t"  "%");
System.out.println("Monthly mortgage payment  \t" $);


}
}
}



C:\Java\MortgageArray.java:10: not a statement
{   {200000, 7, 5.35, 0.0},
^
C:\Java\MortgageArray.java:10: ';' expected
{   {200000, 7, 5.35, 0.0},
^
C:\Java\MortgageArray.java:10: illegal start of expression
{   {200000, 7, 5.35, 0.0},
^
C:\Java\MortgageArray.java:12: ';' expected
{200000, 30, 5.75, 0.0} };
^
C:\Java\MortgageArray.java:14: ')' expected
System.out.println("Mortgage amount is \t" + 0,0$ );
^
C:\Java\MortgageArray.java:15: ')' expected
System.out.println("Term of mortgage is \t" + 0,1 "years");
^
C:\Java\MortgageArray.java:16: ')' expected
System.out.println("Interest rate is \t" 0,3 "%");
^
C:\Java\MortgageArray.java:17: ')' expected
System.out.println("Monthly mortgage payment  \t" 0,4$ );
^
C:\Java\MortgageArray.java:23: 'class' or 'interface' expected
}
^
C:\Java\MortgageArray.java:27: 'class' or 'interface' expected
^
10 errors


Tool completed with exit code 1
:cry:  :confused:

You wanted something like this perhaps?

public class MortgageArray {
  public static void main (String[] args) throws Exception {
    double[][] mortgage = {
      {200000, 7, 5.35, 0.0},
      {200000, 15, 5.5, 0.0},
      {200000, 30, 5.75, 0.0}
    };
    
    System.out.println ("Mortgage amount is \t" + mortgage[0][0]);
    System.out.println ("Term of mortgage is \t" + mortgage[0][1] + "years");
    System.out.println ("Interest rate is \t" + mortgage[0][2] + "%");
    System.out.println ("Monthly mortgage payment \t" + mortgage[0][3] + "$");
  }
}

You wanted something like this perhaps?

public class MortgageArray {
  public static void main (String[] args) throws Exception {
    double[][] mortgage = {
      {200000, 7, 5.35, 0.0},
      {200000, 15, 5.5, 0.0},
      {200000, 30, 5.75, 0.0}
    };
    
    System.out.println ("Mortgage amount is \t" + mortgage[0][0]);
    System.out.println ("Term of mortgage is \t" + mortgage[0][1] + "years");
    System.out.println ("Interest rate is \t" + mortgage[0][2] + "%");
    System.out.println ("Monthly mortgage payment \t" + mortgage[0][3] + "$");
  }
}

Yes, I tried that, but obviously had something wrong. Thanks. So, if I put in another two set of print statements and tell it to give the output for the other two sets of variables, that will work great. I still have the problem with calculating the mortgage payment.

With using normal variabales, the code for calculating the mortgage payment is something like

Payment = (principal * interest) / (1 - Math.pow(1 + interest, -Term of loan)

When I do the calculations, I get an output that is close, but when I use the same calculation with the array method I receive an error that says you can't use *-+/ with arrays. I can't believe that is true. So any suggestions on how to make the calculations work.

I would think that it would be similar except that I think that you insert them in a different location than with just the declared variable method.

Suggestions would be greatly appreciated. I really appreciate the help.

ljraven

>I receive an error that says you can't use *-+/ with arrays
I would wager that you aren't properly subscripting the array. Just remember that array is an array of arrays of type double, array[x] is an array of double, and array[x][y] is a double.

public class MortgageArray4A
{
  public static void main(String[] args) throws Exception
  {

   double[][] mortgage =

   {{200000, 7, 5.35, 0.0},
     {200000, 15, 5.5, 0.0},
     {200000, 30, 5.75, 0.0} };

   //This is what the output should calculate and display

   for (int i = 0; i < 3 ; i++)
    {
   System.out.println ("Mortgage amount is \t" + mortgage[i][0]);
   System.out.println ("Term of mortgage is \t" + mortgage[i][1] +  years");
   System.out.println ("Interest rate is \t" + mortgage[i][2] + "%");
   System.out.println ("Monthly mortgage payment \t$" + (mortgage[i][0] 
   + ((mortgage[i][0] * mortgage[i][2]/100))/(mortgage[i][1]*12)) ");
   }
}

  //My instructor helped me with the last print statement, I knew I was 
  //missing the necessary instructions.  So, now I just have to get it to
  //continue the loop so it display's the rest of the payments until the
  //balance reaches zero.  I appreciate the help that you have provided. :eek:  :)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.