944,171 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 23279
  • Java RSS
Nov 5th, 2004
0

Java Multidimensional Arrays

Expand Post »
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:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ljraven is offline Offline
3 posts
since Nov 2004
Nov 6th, 2004
0

Re: Java Multidimensional Arrays

You wanted something like this perhaps?
Java Syntax (Toggle Plain Text)
  1. public class MortgageArray {
  2. public static void main (String[] args) throws Exception {
  3. double[][] mortgage = {
  4. {200000, 7, 5.35, 0.0},
  5. {200000, 15, 5.5, 0.0},
  6. {200000, 30, 5.75, 0.0}
  7. };
  8.  
  9. System.out.println ("Mortgage amount is \t" + mortgage[0][0]);
  10. System.out.println ("Term of mortgage is \t" + mortgage[0][1] + "years");
  11. System.out.println ("Interest rate is \t" + mortgage[0][2] + "%");
  12. System.out.println ("Monthly mortgage payment \t" + mortgage[0][3] + "$");
  13. }
  14. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 7th, 2004
0

Calculations in Java Multidimensional Arrays

Quote originally posted by Narue ...
You wanted something like this perhaps?
Java Syntax (Toggle Plain Text)
  1. public class MortgageArray {
  2. public static void main (String[] args) throws Exception {
  3. double[][] mortgage = {
  4. {200000, 7, 5.35, 0.0},
  5. {200000, 15, 5.5, 0.0},
  6. {200000, 30, 5.75, 0.0}
  7. };
  8.  
  9. System.out.println ("Mortgage amount is \t" + mortgage[0][0]);
  10. System.out.println ("Term of mortgage is \t" + mortgage[0][1] + "years");
  11. System.out.println ("Interest rate is \t" + mortgage[0][2] + "%");
  12. System.out.println ("Monthly mortgage payment \t" + mortgage[0][3] + "$");
  13. }
  14. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ljraven is offline Offline
3 posts
since Nov 2004
Nov 7th, 2004
0

Re: Java Multidimensional Arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 9th, 2004
0

Re: Java Multidimensional Arrays

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ljraven is offline Offline
3 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC