• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

loops/arrays

Hi. I am trying to modify my original program with an array of 3 mortgage amounts: $200000: 7yr at 5.35, 15 yr at 5.5 and 30 yr at 5.75. I have to show the mortgage payment and then the balance and interest paid. when i declare the new double with the array i get an error that " java:28: array required, but double found". Do i have something wrong?


import java.io.*;
import java.text.DecimalFormat;

class Mortgage
{

public static void main(String arguments[])
      {
/*Declaring Program Variables*/
double interestRate = 0.0575;//*Interest rate
double principal = 200000.00;//*Amount of Loan
double monthlyInterest = interestRate/12; //*Monthly interest
int periods = 360; //* 30 yrs @12 months each year is 360 months
double balance = principal;
double monthlyPrincipal;
DecimalFormat df = new DecimalFormat("$#,###.00");
//Create new decimal format object that has the format required for this program
double mortgageRate[] = {.0535,.0550,.0575};
int payments[] = {84,180,360};

double monthMortgageRate[][]={{mortgageRate[0][0]*12,mortgageRate[0][1]*12,mortgageRate[0][2]*12},  
            {payments[1][0]/1200,payments[1][1]/1200,payments[1][2]/1200}};      
                  
//calculation for mortgage and output that shows payment amounts
double mortgage=(principal*(mortgageRate[i]/12)/(1-(Math.pow(1/(1+mortgageRate[i]/12), (payments[i]*12)))));
System.out.println("Mortgage\t\tMortgage Rate\tPayments\tPayment Amount");
System.out.println((i+1) + "\t\t\t" + (mortgageRate[i]) +
"\t\t" + (payments[i]) +
"\t\t" + df.format(mortgage));

int lines = 0;

for(int i=0; i < 3; i++)
             {
             
             //new array for balance and interest
             double monthlyInterest=principal * monthMortgageRate[1][i];
             double newBalance=principal -(mortagage - monthlyInterest);
             double mortgage = newBalance;
             
             System.out.println("\n" +i+ "\t\t" + "\t\t\t" +string);
             
   

            }
      }
}

0
pgmtkl
Asked:
pgmtkl
  • 8
  • 7
  • 2
  • +1
3 Solutions
 
SStoryCommented:
I think there are already financial functions to do this for you without need of a loop.  VB6 had them.  I'm not 100% sure on .NET, but I bet they are still there.
0
 
WelkinMazeCommented:
Hi,
you are using mortgageRate and payments arrays as bidimensional arrays but they are not!

double monthMortgageRate[][]={{mortgageRate[0][0]*12,mortgageRate[0][1]*12,mortgageRate[0][2]*12},  
          {payments[1][0]/1200,payments[1][1]/1200,payments[1][2]/1200}};
0
 
pgmtklAuthor Commented:
i have to show the payments for each and then another list for the balance and interest. that isn a multi dimensional array? how would i do that then?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
WelkinMazeCommented:
You can do it both ways - with one multidimensional array or with several one dimensional.
I've meant with my previous posting that you have declared the arrays as one dimensional
double mortgageRate[] = {.0535,.0550,.0575};
int payments[] = {84,180,360};

but you've used them az bidimendional.
So this has tobe the source for the error you've mentioned above.
0
 
pgmtklAuthor Commented:
ok i see, i didnt realize i could use the first. I removed the bidimensional and i get an error when trying to calculate the balance and interest. I have listed my updated code. i get 'array required, but double found'. Am i still doing something wrong?


import java.io.*;
import java.text.DecimalFormat;

class Mortgage
{

public static void main(String arguments[])
      {
/*Declaring Program Variables*/
double interestRate = 0.0575;//*Interest rate
double principal = 200000.00;//*Amount of Loan
double monthlyInterest = interestRate/12; //*Monthly interest
int periods = 360; //* 30 yrs @12 months each year is 360 months
double balance = principal;
double monthlyPrincipal;
DecimalFormat df = new DecimalFormat("$#,###.00");
//Create new decimal format object that has the format required for this program
double mortgageRate[] = {.0535,.0550,.0575};
int payments[] = {84,180,360};


int lines = 0;

for(int i=0; i < 3; i++)
             {
//calculation for mortgage and output that shows payment amounts
double mortgage=(principal*(mortgageRate[i]/12)/(1-(Math.pow(1/(1+mortgageRate[i]/12), (payments[i]*12)))));
System.out.println("Mortgage\t\tMortgage Rate\tPayments\tPayment Amount");
System.out.println((i+1) + "\t\t\t" + (mortgageRate[i]) +
"\t\t" + (payments[i]) +
"\t\t" + df.format(mortgage));

//new calculation for balance and interest
double newMonthlyInterest=principal * mortgageRate[1][i];
double newBalance=principal -(mortagage - newMonthlyInterest);
double mortgage = newBalance;

System.out.println("\n" +i+ "\t\t" + "\t\t\t" +string);

            }
      }
}

0
 
suprapto45Commented:
Hi,

Your mortgageRate is no longer two dimensional array but you still use it as two dimensional array.

double newMonthlyInterest=principal * mortgageRate[1][i];               // still two dimensional array
double newBalance=principal -(mortagage - newMonthlyInterest);      // wrong spelling
double mortgage = newBalance;                                                     // duplication of mortgage...you already have mortgage before

System.out.println("\n" +i+ "\t\t" + "\t\t\t" +string);                           // where is the string is declared?


Is this what you want?

double newMonthlyInterest=principal * mortgageRate[i];
double newBalance=principal -(mortgage - newMonthlyInterest);
mortgage = newBalance;

System.out.println("\n" +i+ "\t\t" + "\t\t\t" +mortgage);

David
0
 
pgmtklAuthor Commented:
that worked. it shows the mortgages. why is it not showing the balance and interest for the whole loan?
0
 
suprapto45Commented:
You have to print it out using something like
System.out.println("\n" +i+ "\t\t" + "\t\t\t" +mortgage);

David

0
 
pgmtklAuthor Commented:
It is printing just the first payment. My calculation i thought was to take the initiial payment and reduce to get the new balance until the loan is paid off. (list all the payments). i have one calculation for mortgage and then the next calculation to figure balance. Is it in the wrong order? sorry for all the questions/
0
 
suprapto45Commented:
Let me see the logics. Just now I just *fixed* your codes by assuming that all the logics are correct

David
0
 
suprapto45Commented:
Okay, show me your latest codes and clearly define what's wrong with that.

David
0
 
pgmtklAuthor Commented:
Hi here is my latest code. i changed the last utput line to display with df.format which was fine:

import java.io.*;
import java.text.DecimalFormat;

class Mortgage
{

public static void main(String arguments[])
      {
/*Declaring Program Variables*/
double interestRate = 0.0575;//*Interest rate
double principal = 200000.00;//*Amount of Loan
double monthlyInterest = interestRate/12; //*Monthly interest
int periods = 360; //* 30 yrs @12 months each year is 360 months
double balance = principal;
double monthlyPrincipal;
DecimalFormat df = new DecimalFormat("$#,###.00");
//Create new decimal format object that has the format required for this program
double mortgageRate[] = {.0535,.0550,.0575};
int payments[] = {84,180,360};


int lines = 0;

for(int i=0; i < 3; i++)
             {
//calculation for mortgage and output that shows payment amounts
double mortgage=(principal*(mortgageRate[i]/12)/(1-(Math.pow(1/(1+mortgageRate[i]/12), (payments[i]*12)))));
System.out.println("Mortgage\t\tMortgage Rate\tPayments\tPayment Amount");
System.out.println((i+1) + "\t\t\t" + (mortgageRate[i]) +
"\t\t" + (payments[i]) +
"\t\t" + df.format(mortgage));

//new array for balance and interest
double newMonthlyInterest=principal * mortgageRate[i];
double newBalance=principal -(mortgage - newMonthlyInterest);
mortgage = newBalance;

System.out.println("\n" +i+ "\t\t" + "\t\t\t" +df.format(mortgage));
System.out.println("\n" +i+ "\t\t" + "\t\t\t" +df.format(mortgage));



            }
      }
}
//end of program

------=-

This below part calculates the mortgage for each of the 3 types and rates(at top of logic):
double mortgage=(principal*(mortgageRate[i]/12)/(1-(Math.pow(1/(1+mortgageRate[i]/12), (payments[i]*12)))));
System.out.println("Mortgage\t\tMortgage Rate\tPayments\tPayment Amount");

This part i think is supposed to calculate every payment for each of the above mortgage payments- list balance and interest paid until it is $0:
double newMonthlyInterest=principal * mortgageRate[i];
double newBalance=principal -(mortgage - newMonthlyInterest);
mortgage = newBalance;



When i compile it is succesfull but this is the output i get this below. It is like it stops after 1st payment:

Mortgage            Mortgage Rate      Payments      Payment Amount
1                  0.0535            84            $901.85

0                              $209,798.15
Mortgage            Mortgage Rate      Payments      Payment Amount
2                  0.055            180            $916.71

1                              $210,083.29
Mortgage            Mortgage Rate      Payments      Payment Amount
3                  0.0575            360            $958.33

2                              $210,541.67



0
 
suprapto45Commented:
Okay,

What should be the output? Sorry...do not really know the logic of mortgage :)

David
0
 
pgmtklAuthor Commented:
you are helping me, thanks. The output should display the mortage and then all of the payments for each of the loan- payment, balance and interest.
0
 
suprapto45Commented:
Not a prob :).

Can you provide me with the *real* number of output?

>>"Mortgage          Mortgage Rate     Payments     Payment Amount
1               0.0535          84          $901.85

0                         $209,798.15
Mortgage          Mortgage Rate     Payments     Payment Amount
2               0.055          180          $916.71

1                         $210,083.29
Mortgage          Mortgage Rate     Payments     Payment Amount
3               0.0575          360          $958.33

2                         $210,541.67"

Are these correct? If not, can you provide me with something like above but the correct version

David
0
 
pgmtklAuthor Commented:
they are correct. the 3 mortgages are 7yr @5.35%, 15 year at 5.5% and 30 yr at 5.75%. For the 7yr we are supposed to list the 84 payments with balance and interest, same for the others.
0
 
suprapto45Commented:
Are you sure that for the first 7yr @5.35%, it is $901.85 per month. I check with http://www.mortgage-calc.com/mortgage/simple.php and it is incorrect

David
0
 
pgmtklAuthor Commented:
There may be an error with the calculation and i think that is why it is not working. What in my calcultion seems to be wrong?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 8
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now