I am not sure if I am doing this correctly since it has been awhile since I last used Daniweb. I need some help with the code that I have submitted and am getting the following two errors. I am new to java and not sure of what to do. Can someone please help me to get my code to compile and run as expected?

/*Program: MortgagePaymentCalculatorCR3.java
Purpose: To write a Java program (without GUI) for agents of McBride Financial
           Services. This resource will calculate and display the payment expected
           for 3 mortgage loans with the following terms and interest rates using
           an array:
                    7 year @ 5.35%
                    15 year @ 5.5%
                    30 year @ 5.75%

           The loan balance and interest paid for each payment over the duration of
           each loan will also be displayed. Loops will display part of the list,
           pause, and display more of the list to the agent.
*/

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class MortgageCalculatorExample
{
    public static void main(String[] args)
    {
        //Define variables for array
        double [] yearlyTerm = {7, 15, 30};        //mortgage term (total years)
        double [] monthlyTerm = {84, 180, 360};    //mortgage terms (total months)
        double [] apr = {.0535, .055, .0575};   //annual percentage rate

        //Declare additional Variables
        double principal;                     //loan amount
        double paymentFormula;                //factor to calculate monthly payment
        double interestRate;                  //interest rate
        double monthlyPayment;                //monthly payment


                 int term;                     //variable part of notePeriod
                 int linecount;                //to display lines
                 double monthlyInterest;       //monthly interest
         double monthlyPrincipal;      //monthly principal
         double loanBalance;

        //initialize variables
        principal = 200000;                   //loan amount
        paymentFormula = 0;                   //initializes value for paymentFormula
        interestRate = 0;                     //initializes value for interestRate
        monthlyPayment = 0;                   //initializes value for monthlyPayment

        loanBalance = principal;

        //Loop for array
        int i;

        for (i = 0; i <= 2; i ++) {
            //formula to calculate interest rate
            interestRate = apr[i] / 12; //monthly interest rate

            //formula to calculate paymentFormula
            paymentFormula = (Math.pow((1 + interestRate), monthlyTerm[i]) -1)/
                         (interestRate * Math.pow((1 + interestRate), monthlyTerm[i]));

            //formula to calculate the monthly payment
            monthlyPayment = principal / paymentFormula;

            //format numbers
            NumberFormat formatter = new DecimalFormat("#0.00");

            //output
            System.out.println("Amount borrowed: $" + formatter.format(principal));
            System.out.println("Mortgage term (years): " + yearlyTerm[i]);
            System.out.println("Interest rate (percent): " + apr[i] * 100);
            System.out.println("Monthly mortgage payment amount: $" + formatter.format(monthlyPayment));
            System.out.println();



            //format table
            System.out.println("\nPayments Left\t Interest Paid\t Loan Balance");
            System.out.println("--------------  --------------  --------------");

             //while loop
                     while (monthlyTerm > 0) {
                        //data output for table
                        System.out.println(monthlyTerm + "\t\t$"
                                                          + formatter.format(monthlyInterest)
                                                          + "\t\t$" + formatter.format(loanBalance));

                        //Decrement term of mortgage loan
                        monthlyTerm++;

                        //declare monthly interest and principal and loan balance in while loop
                        monthlyInterest = loanBalance * interestRate;
                        monthlyPrincipal = monthlyPayment - monthlyInterest;
                        loanBalance = loanBalance - monthlyPrincipal;

                        //Pause output
                        if(linecount == 20) {
                            linecount = 0;
                            try {
                                Thread.sleep(2000);
                                }
                            catch (InterruptedException e) {
                                }
                            }
                        else {
                            linecount++;
                             }
                    }
               }
}

                }





C:\Users\Momma Bear\Documents\UOP\PRG420\Week 5\MortgageCalculatorExample.java:83: operator > cannot be applied to double[],int
                     while (monthlyTerm > 0) {
                                        ^
C:\Users\Momma Bear\Documents\UOP\PRG420\Week 5\MortgageCalculatorExample.java:90: operator ++ cannot be applied to double[]
                        monthlyTerm++;
                                   ^

2 errors

Thanks in advance.

operator > cannot be applied to double[],int

You can not compare an array to an int value. Did you mean to have an index (in []s) with the array name to select an element from the array?

The same problem with the other error. You can not use ++ with an array. You must select an element by using the [] notation.

Thanks sooooo much. I made some changes and finally got it to work. The only issue is that the information for payment, interest, and loan balance is not displaying in the columns under each loan mortgage. Can someone please help since I am not sure of what I should be doing to make this happen? Everything I have done is not adding the data as expected.

Thanks in advance

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class MortgageCalculator
{
    public static void main(String[] args)
    {
        //Define variables for array
        double [] yearlyTerm = {7, 15, 30};        
        double [] monthlyTerm = {84, 180, 360};    
        double [] apr = {.0535, .055, .0575};      

        //Declare additional Variables
        double principal;                          
        double paymentFormula;                    
        double interestRate;                      
        double monthlyPayment;                    
        int term;                                
        int linecount;                           
        double monthlyInterest;                  
        double monthlyPrincipal;                
        double loanBalance;                     
        int notePeriod;

        //initialize variables
        principal = 200000;                   
        paymentFormula = 0;                   
        interestRate = 0;                     
        monthlyPayment = 0;                   
        monthlyInterest = 0;                  
        term = 30;                            
        notePeriod = term * 12;               
        loanBalance = principal;              
        linecount = 20;                         

        //Loop for array
        int i;

        for (i = 0; i <= 2; i ++) {
            //formula to calculate interest rate
            interestRate = apr[i] / 12; //monthly interest rate

            //formula to calculate paymentFormula
            paymentFormula = (Math.pow((1 + interestRate), monthlyTerm[i]) -1)/
                         (interestRate * Math.pow((1 + interestRate), monthlyTerm[i]));

            //formula to calculate the monthly payment
            monthlyPayment = principal / paymentFormula;

            //format numbers
            NumberFormat formatter = new DecimalFormat("#0.00");

            //output
            System.out.println("Amount borrowed: $" + formatter.format(principal));
            System.out.println("Mortgage term (years): " + yearlyTerm[i]);
            System.out.println("Interest rate (percent): " + apr[i] * 100);
            System.out.println("Monthly mortgage payment amount: $" + formatter.format(monthlyPayment));
            System.out.println();

            //format table
            System.out.println("\nPayments Left\t Interest Paid\t Loan Balance");
            System.out.println("--------------  --------------  --------------");

            //while loop
            while (monthlyTerm[0] > 84) {       // Check the 1st index of array]
                System.out.println("\nPayments Left\t Interest Paid\t Loan Balance");
                System.out.println("--------------  --------------  --------------");

                //data output for table
                System.out.println(monthlyTerm + "\t\t$"
                                               + formatter.format(monthlyInterest)
                                               + "\t\t$" + formatter.format(loanBalance));

            while (monthlyTerm[1] > 180) {      // Check the 2nd index of array
                //data output for table
                System.out.println(monthlyTerm + "\t\t$"
                                               + formatter.format(monthlyInterest)
                                               + "\t\t$" + formatter.format(loanBalance));

            while (monthlyTerm[2] > 360) {     // Check the 3rd index of array
                //data output for table
                System.out.println(monthlyTerm + "\t\t$"
                                               + formatter.format(monthlyInterest)
                                               + "\t\t$" + formatter.format(loanBalance));
                                          }
                    }
                }
            }

    }
}

not displaying in the columns under each loan mortgag

Please post the program's output that shows the problem. Using tabs can be tricky. Blank fill could be more reliable.

I am not sure how to do that since the does compile and run. I am able to see the issue once it displays in the console window. Am I missing something? Please bear with me. I am new to Java.

On Windows:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Thanks. I did as you instructed and here is the output. The data should appear under the headings payments left, interest paid, and loan balance for each of the 3 loans.

Amount borrowed: $200000.00
Mortgage term (years): 7.0
Interest rate (percent): 5.35
Monthly mortgage payment amount: $2859.79

Payments Left Interest Paid Loan Balance
-------------- -------------- --------------
Amount borrowed: $200000.00
Mortgage term (years): 15.0
Interest rate (percent): 5.5
Monthly mortgage payment amount: $1634.17

Payments Left Interest Paid Loan Balance
-------------- -------------- --------------
Amount borrowed: $200000.00
Mortgage term (years): 30.0
Interest rate (percent): 5.75
Monthly mortgage payment amount: $1167.15

Payments Left Interest Paid Loan Balance
-------------- -------------- --------------
Press any key to continue . . .

The data should appear under the headings

Can you explain what is wrong with the printout? I don't see anything that looks like it should be in columns.

Is the problem that the program is NOT printing anything? Look at the conditions in the while() statement loops where there are some println statements. When will any of those conditions be true? If they are ever true, when would they go false so that the loop would end?

Please describe what the conditions should be for the code in those loops to be executed and when the loop should end so it does not print forever.

Exactly. The program should print the loan balance and interest paid for each payment over the term of the loan. I hope I am saying this clearly, but the conditions should be executed once the mortgage payment is calculated. For example, once the first mortgage that has a 7 year term with 5.35% interest is calculated, the balance and interest for that loan should print. Basically, I should see data for 84 payments before moving to the next loan. The loop should end once the loan balance is 0. Did that help? I have looked at my statements to try to figure this out and its not coming to me. Unfortunately, I am getting the same results.

 while (monthlyTerm[0] > 84) {

when will the above condition ever be true?
Look at the other while statements and ask the same question.

what do you want the code to do inside the loop? How many times should the loop go around? What will change from one iteration of the loop to the next one?

Ok. I have been at this for the last few hours. I made some changes and finally got the output I needed but cannot figure out how to get it to stop and move to the next loan conditions for the program

Amount borrowed: $200000.00
Mortgage term (years): 7.0
Interest rate (percent): 5.35
Monthly mortgage payment amount: $2859.79


Payment Number   Interest Paid   Loan Balance
--------------  --------------  --------------
0               $0.00           $200000.00

Payment Number   Interest Paid   Loan Balance
--------------  --------------  --------------
1               $891.67         $198031.88

Payment Number   Interest Paid   Loan Balance
--------------  --------------  --------------
2               $882.89         $196054.98

Payment Number   Interest Paid   Loan Balance
--------------  --------------  --------------
3               $874.08         $194069.27

Payment Number   Interest Paid   Loan Balance
--------------  --------------  --------------
4               $865.23         $192074.70

Payment Number   Interest Paid   Loan Balance
--------------  --------------  --------------
5               $856.33         $190071.24

This should stop once the payment number gets to 84 which is when the balance will be 0, but it just keeps running. I then need it to calculate and pring the next loan term and amount. Please help. Here is the code I have

public class MortgageCalculator
{
    public static void main(String[] args)
    {
        //Define variables for array
        double [] yearlyTerm = {7, 15, 30};        //mortgage term (total years)
        double [] monthlyTerm = {84, 180, 360};    //mortgage terms (total months)
        double [] apr = {.0535, .055, .0575};      //annual percentage rate

        //Declare additional Variables
        double principal;                          //loan amount
        double paymentFormula;                    //factor to calculate monthly payment
        double interestRate;                      //interest rate
        double monthlyPayment;                    //monthly payment
        int term;                                //variable part of notePeriod
        int linecount;                           //to display lines
        double monthlyInterest;                  //monthly interest
        double monthlyPrincipal;                //monthly principal
        double loanBalance;                      //loan balance
        int notePeriod;

        //initialize variables
        principal = 200000;                   //loan amount
        paymentFormula = 0;                   //initializes value for paymentFormula
        interestRate = 0;                     //initializes value for interestRate
        monthlyPayment = 0;                   //initializes value for monthlyPayment
        monthlyInterest = 0;                   //initialize value for monthlyInterest
        term = 30;                            //initialize value for term
        notePeriod = term * 12;                 //initialize value for notePeriod
        loanBalance = principal;                //initialize value for loanBalance
        linecount = 0;                         //initialize value for linecount

        //Loop for array

        for (notePeriod = 0; notePeriod <= 0; notePeriod--) {
            //formula to calculate interest rate
            interestRate = apr[notePeriod] / 12; //monthly interest rate

            //formula to calculate paymentFormula
            paymentFormula = (Math.pow((1 + interestRate), monthlyTerm[notePeriod]) -1)/
                         (interestRate * Math.pow((1 + interestRate), monthlyTerm[notePeriod]));

            //formula to calculate the monthly payment
            monthlyPayment = principal / paymentFormula;

            //format numbers
            NumberFormat formatter = new DecimalFormat("#0.00");

            //output
            System.out.println("Amount borrowed: $" + formatter.format(principal));
            System.out.println("Mortgage term (years): " + yearlyTerm[notePeriod]);
            System.out.println("Interest rate (percent): " + apr[notePeriod] * 100);
            System.out.println("Monthly mortgage payment amount: $" + formatter.format(monthlyPayment));
            System.out.println();

            //while loop
            while (monthlyTerm[0] <= 84) {    // Check the 1st index of array

                System.out.println("\nPayment Number\t Interest Paid\t Loan Balance");
                System.out.println("--------------  --------------  --------------");


                //data output for table
                System.out.println(notePeriod++ + "\t\t$"
                                               + formatter.format(monthlyInterest)
                                               + "\t\t$" + formatter.format(loanBalance));


                 monthlyInterest = loanBalance * interestRate;
                 monthlyPrincipal = monthlyPayment - monthlyInterest;
                loanBalance = loanBalance - monthlyPrincipal;


                if(linecount == 5) {
                    linecount = 0;
                    try {
                    Thread.sleep(2000);
                    }
                catch (InterruptedException e) {
                    }
                }
                else {
                    linecount++;
                 }


            while (monthlyTerm[1] > 180) {      // Check the 2nd index of array

                 System.out.println("\nPayments Left\t Interest Paid\t Loan Balance");
                 System.out.println("--------------  --------------  --------------");


                //data output for table
                System.out.println(monthlyTerm + "\t\t$"
                                               + formatter.format(monthlyInterest)
                                               + "\t\t$" + formatter.format(loanBalance));

                monthlyInterest = loanBalance * interestRate;
                                 monthlyPrincipal = monthlyPayment - monthlyInterest;
                                loanBalance = loanBalance - monthlyPrincipal;


            while (monthlyTerm[2] > 360) {     // Check the 3rd index of array
                //data output for table
                System.out.println(monthlyTerm + "\t\t$"
                                               + formatter.format(monthlyInterest)
                                               + "\t\t$" + formatter.format(loanBalance));
                                          }
                    }
                }
            }

    }
}

Why are you printing a new heading for each line? Is that what you want? Normally there would be one heading per page.
When should the loop finish printing and exit? What variable's value can you use to determine when the loop should exit? EIther test for that ending value in the while() condition or use an if statement with a break statement.

Honestly, I do not need a new heading for each line but could not figure out how to get it to print one heading per page so I left it as it was for now. The loop should finish once the balance of 0 is reached for the first loan. So once the end of the term is reached, the loop should exit and move to the next loan and calculate using those set of terms. This process should continue until all data is printed for each of the 3 loans. I just tried to use an if statement with a break statement, but that did not work out too well. I know I am doing something wrong. It did not print any of the data in the columns.

I just got the loop to stop. I ended up using a if statement with a break statement. I sat here for about 30 seconds staring at the output when it hit me that I was using the wrong variable. I know have to figure out how to get the second terms of the loan to calculate and print.

It looks like there is a for loop that should go around 3 times, once for each term etc.

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.