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

x
?
Solved

loops/arrays

Posted on 2006-05-01
18
Medium Priority
?
268 Views
Last Modified: 2010-04-17
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
Comment
Question by:pgmtkl
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 200 total points
ID: 16580860
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
 
LVL 11

Assisted Solution

by:WelkinMaze
WelkinMaze earned 200 total points
ID: 16580992
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
 

Author Comment

by:pgmtkl
ID: 16581035
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
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.

 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16581081
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
 

Author Comment

by:pgmtkl
ID: 16582232
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
 
LVL 16

Accepted Solution

by:
suprapto45 earned 1600 total points
ID: 16582394
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
 

Author Comment

by:pgmtkl
ID: 16582581
that worked. it shows the mortgages. why is it not showing the balance and interest for the whole loan?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16582627
You have to print it out using something like
System.out.println("\n" +i+ "\t\t" + "\t\t\t" +mortgage);

David

0
 

Author Comment

by:pgmtkl
ID: 16582654
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 16582671
Let me see the logics. Just now I just *fixed* your codes by assuming that all the logics are correct

David
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 16582689
Okay, show me your latest codes and clearly define what's wrong with that.

David
0
 

Author Comment

by:pgmtkl
ID: 16582716
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 16582721
Okay,

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

David
0
 

Author Comment

by:pgmtkl
ID: 16582743
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 16582762
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
 

Author Comment

by:pgmtkl
ID: 16582791
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 16582831
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
 

Author Comment

by:pgmtkl
ID: 16585491
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question