I figure my formulas are wrong and need tweaking. Can someone help

package mortgage_calculator;

/* Write the program in Java (without a graphical user interface) using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term. Display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan.
 If the list would scroll off the screen, use loops to display a partial list, hesitate, and then display more of the list. */
import java.lang.Math.*;
import java.text.NumberFormat;

/* Program uses the Math class to perform mathematical functions. The math class will be called to raise a portion of code to a specified power. */

public class Main //starting point

    public static void main(String[] args)              //tells the program how to perform task or function
        double dblMonthlyPayment;                       // declared variable dblpayment as a double
        double dblLoanAmount=200000;                     // declared variable dblprincipal as a double
        int intLoanTermYears=30;                                // declared variable intyears as an interger
        int intLoanTermInMonths;                             // declared variable intMonthlength as an interger
        double dblInterest=5.75;                        // declared variable dblinterest as a double
        double dblMonthlyInterest = 0;                  // declared variable dblmonthlyInterest as a double
        double dblLoanBalance = 0;
        double dblNewLoanBalance = 0;
        double dblCurrentMonthlyLoanBalance = 0;
        double dblCurrentMonthlyInterest = 0;
        NumberFormat nf = NumberFormat.getCurrencyInstance();

        /* P = principal, the initial amount of the loan
           I = the annual interest rate (from 1 to 100 percent)
           L = length, the length (in years) of the loan, or at least the length over which the loan is amortized.
           J = monthly interest in decimal form = I / (12 x 100)
           N = number of months over which loan is amortized = L x 12
           M = the monthly payment

           M = P * ( J / (1 - (1 + J) ** -N))

           The terms above are used to form an equation that will calculate the monthly payment.

        dblMonthlyInterest = dblInterest/(12*100); //J
        intLoanTermInMonths = intLoanTermYears*12;   //N
        dblMonthlyPayment = dblLoanAmount*(dblMonthlyInterest / (1 - Math.pow((1 + dblMonthlyInterest), -intLoanTermInMonths))); //           M = P * ( J / (1 - (1 + J) ** -N))

    System.out.println("*************************************");                    //Used for spacing
            System.out.println();                                               //Used for spacing
            System.out.println("The principal of the loan is $200,000");        // displays literal
            System.out.println("The length of the loan is 30 years");           // displays literal
            System.out.println("The interest on the loan is 5.75%");            // displays literal
            System.out.println("The monthly payment is "  + nf.format (dblMonthlyPayment));  // displays literal and monthly payment by concatenate the literal with the variable
            System.out.println();                                               //Used for spacing
            System.out.println("*************************************");        //Used for spacing
                /* Calculation formulas*/
                dblLoanBalance = dblLoanAmount - dblMonthlyPayment;
                dblCurrentMonthlyInterest = dblLoanBalance * dblMonthlyInterest;
                dblCurrentMonthlyLoanBalance = dblLoanBalance + dblCurrentMonthlyInterest;
                dblNewLoanBalance = dblCurrentMonthlyLoanBalance;
                dblLoanBalance = dblLoanBalance - dblMonthlyPayment;



                System.out.println("Display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan.");
                System.out.println("Monthly Payment" +"\t\t" + "Loan Balance" + "\t\t" + "Interest Paid");

                int intlinecounter=1;
                for(intlinecounter=1; intlinecounter<=intLoanTermInMonths; intlinecounter++)
                    dblCurrentMonthlyInterest = dblLoanBalance * dblMonthlyInterest;
                dblCurrentMonthlyLoanBalance = dblLoanBalance + dblCurrentMonthlyInterest;
               dblLoanBalance = dblLoanBalance - dblMonthlyPayment;
                dblNewLoanBalance = dblCurrentMonthlyLoanBalance;

           if(intlinecounter % 20 == 0)
{              intlinecounter=1;
               System.out.println("Press any key to continue");
                                {  System.in.read();
                                }catch (Exception e){;
                                System.out.println (intlinecounter +"\t"+ nf.format(dblMonthlyPayment)+ "\t\t" + nf.format(dblNewLoanBalance)+ "\t\t"+ nf.format (dblCurrentMonthlyInterest));




Yes, I think your formulas are incorrect, since the program calculates a negative balance after about 14 years. :)

You can get the program to pause using Thread.sleep(n);, where n is a number of milliseconds, rather than asking the user to hit a key. This needs to go in a try block, just like System.in.read().

Be a part of the DaniWeb community

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