Hi,

I am trying to create a mortgage calculator per an assignment and have it working ALMOST perfectly. I just need to add a loop to make it pause when the screen is filled and wait for an ENTER until the loan balance is 0. Can someone help me?

Here is what I have:

import java.text.*; // FOR THE FORMATTERS

public class MortgageProgram {
    // FORMATTERS: see use in functions print(), and showAmortization().
    NumberFormat currency = NumberFormat.getCurrencyInstance(); // currency format.
    DecimalFormat intRate = new DecimalFormat("##.00"); // interest rate format.

    // ADDED:  rate, months
    double payment = 0.0, rate = 0.0;
    double principal = 200000.0; //*Principle amount of loan is $200,000
    double annualInterest = 0.0575; //*Interest rate is currently 5.75%
    int years = 30; //*Term of the loan is 30 years
    int months = 0; // years expressed in months

        public static void main (String[] args){
        MortgageProgram MP = new MortgageProgram(args);
        }

        public MortgageProgram(String[] args)   {
           if (args.length == 3) {
           principal = Double.parseDouble(args[0]);
           annualInterest = Double.parseDouble(args[1])/100; // AUTO-CONVERT -- this assumes
                                // the user enters as 5.75, etc.
                                // as your statement in showUsage()
                                // implies.
           years = Integer.parseInt(args[2]);
               print();
           }
           else
           {
               showUsage();
       } 
    }

        public void calculatePayment(){ // ELIMINATED ARGUMENTS and, RETURN TYPE CHANGED TO VOID.
        rate = annualInterest / 12;
        months = years * 12;    // USE NEW VARS.
            payment = (principal * rate)
                    / (1 - Math.pow(1/ (1 + rate), months)); //*Shows 1 monthly payment multiplied by 12 to make one complete year.
        }


    public void print(){ // ELIMINATED ARGUMENTS
        calculatePayment();
        System.out.println("The principal is " + currency.format(principal)); //*Shows the principle amount in $ value.
        System.out.println("The annual interest rate is " + intRate.format(annualInterest * 100) +"%");
        System.out.println("The term is " + years + " years"); //*Term is normally in years.
        System.out.println("Your monthly payment is " + currency.format(payment)); //*Shows output of monthly payment.
    showAmortization();
    }

    public void showUsage() {
    System.out.println("Usage: Calculate Mortgage Payment Amount ");
    System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term.
    System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
    print();
    System.exit(0); //*System exits nice and quietly
    }

    public void showAmortization()  {
    double pmt2interest = 0.0;  // payment-to-interest portion.
    double pmt2principl = 0.0;  // payment-to-principal portion.
    double pmtBalance = 0.0;

        System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance");
        System.out.println("====\t===========\t===========\t=======");

    for ( int i = 1; i <= months; ++i )
    {
        pmt2interest = principal * rate;
        pmt2principl = payment - pmt2interest;
        pmtBalance = principal - pmt2principl;

        System.out.println( "#" + i
            + "\tP=" + currency.format(pmt2principl)
            + "\tI=" + currency.format(pmt2interest)
            + (pmt2interest < 10 ? "\t\tB=" : "\tB=")  // tab adjustment.
            + currency.format(pmtBalance) );

        principal -= pmt2principl; // reduce by our payment-to-principal

    }
    }
}

Edited 3 Years Ago by pritaeas: Fixed formatting

Code tags please. Makes it much easier to read.

[code]

// paste code here

[/code]

or

[code=JAVA] // paste code here

[/code]


Figure out where you want to pause, then use a Scanner object to read in input from the user, which forces the program to pause for user input. If you don't need the input and just need to pause, just read in the input and don't do anything with it. The link above has a few examples at the top.

Sorry about not wrapping the code. This was my first time here. Code is below with the code wrapper.

I am looking to just have a loop that pauses the output, just not sure how to do it right.

import java.text.*; // FOR THE FORMATTERS

public class MortgageProgram {
// FORMATTERS: see use in functions print(), and showAmortization().
NumberFormat currency = NumberFormat.getCurrencyInstance(); // currency format.
DecimalFormat intRate = new DecimalFormat("##.00"); // interest rate format.

// ADDED: rate, months
double payment = 0.0, rate = 0.0;
double principal = 200000.0; //*Principle amount of loan is $200,000
double annualInterest = 0.0575; //*Interest rate is currently 5.75%
int years = 30; //*Term of the loan is 30 years
int months = 0; // years expressed in months

public static void main (String[] args){
MortgageProgram MP = new MortgageProgram(args);
}

public MortgageProgram(String[] args) {
if (args.length == 3) {
principal = Double.parseDouble(args[0]);
annualInterest = Double.parseDouble(args[1])/100; // AUTO-CONVERT -- this assumes
// the user enters as 5.75, etc.
// as your statement in showUsage()
// implies.
years = Integer.parseInt(args[2]);
print();
}
else
{
showUsage();
} 
}

public void calculatePayment(){ // ELIMINATED ARGUMENTS and, RETURN TYPE CHANGED TO VOID.
rate = annualInterest / 12;
months = years * 12; // USE NEW VARS.
payment = (principal * rate)
/ (1 - Math.pow(1/ (1 + rate), months)); //*Shows 1 monthly payment multiplied by 12 to make one complete year.
}


public void print(){ // ELIMINATED ARGUMENTS
calculatePayment();
System.out.println("The principal is " + currency.format(principal)); //*Shows the principle amount in $ value.
System.out.println("The annual interest rate is " + intRate.format(annualInterest * 100) +"%");
System.out.println("The term is " + years + " years"); //*Term is normally in years.
System.out.println("Your monthly payment is " + currency.format(payment)); //*Shows output of monthly payment.
showAmortization();
}

public void showUsage() {
System.out.println("Usage: Calculate Mortgage Payment Amount ");
System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term.
System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
print();
System.exit(0); //*System exits nice and quietly
}

public void showAmortization() {
double pmt2interest = 0.0; // payment-to-interest portion.
double pmt2principl = 0.0; // payment-to-principal portion.
double pmtBalance = 0.0;

System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance");
System.out.println("====\t===========\t===========\t=======");

for ( int i = 1; i <= months; ++i )
{
pmt2interest = principal * rate;
pmt2principl = payment - pmt2interest;
pmtBalance = principal - pmt2principl;

System.out.println( "#" + i
+ "\tP=" + currency.format(pmt2principl)
+ "\tI=" + currency.format(pmt2interest)
+ (pmt2interest < 10 ? "\t\tB=" : "\tB=") // tab adjustment.
+ currency.format(pmtBalance) );

principal -= pmt2principl; // reduce by our payment-to-principal

}
}
}

Just increment an int until it is less than a certain value or something if you don't want to take Vernon's advice.

int i = 0;
do{
i++;
} while(i<10000000)

Hi,

Thanks for your help for a real beginner. I think I've almost got it, but for some reason it stops after one payment showing the interest paid and balance, but I need it to repeat for all.

Any more help would be appreciated.

import java.text.*; // FOR THE FORMATTERS

public class mortgageprogram {
	// FORMATTERS: see use in functions print(), and showAmortization().
	NumberFormat currency = NumberFormat.getCurrencyInstance(); // currency format.
	DecimalFormat intRate = new DecimalFormat("##.00");	// interest rate format.

	// ADDED:  rate, months
	double payment = 0.0, rate = 0.0;
	double principal = 200000.0; //*Principle amount of loan is $200,000
	double annualInterest = 0.0575; //*Interest rate is currently 5.75%
	int years = 30; //*Term of the loan is 30 years
	int months = 0; // years expressed in months

        public static void main (String[] args){
		mortgageprogram MP = new mortgageprogram(args);
        }

        public mortgageprogram(String[] args)	{
           if (args.length == 3) {
	       principal = Double.parseDouble(args[0]);
	       annualInterest = Double.parseDouble(args[1])/100; // AUTO-CONVERT -- this assumes
		   						// the user enters as 5.75, etc.
		   						// as your statement in showUsage()
		   						// implies.
	       years = Integer.parseInt(args[2]);
               print();
           }
           else
           {
               showUsage();
	   }
	}

        public void calculatePayment(){
	    rate = annualInterest / 12;
	    months = years * 12;
            payment = (principal * rate)
                    / (1 - Math.pow(1/ (1 + rate), months)); //*Shows 1 monthly payment multiplied by 12 to make one complete year.
        }

    public void print(){
        calculatePayment();
        System.out.println("The principal is " + currency.format(principal)); //*Shows the principle amount in $ value.
        System.out.println("The annual interest rate is " + intRate.format(annualInterest * 100) +"%");
        System.out.println("The term is " + years + " years"); //*Term is normally in years.
        System.out.println("Your monthly payment is " + currency.format(payment)); //*Shows output of monthly payment.
	showAmortization();
    }

    public void showUsage() {
	System.out.println("Usage: Calculate Mortgage Payment Amount ");
	System.out.println("\nFor example: $200,000 Mortgage loan amount, 5.75% interest for a 30 year term "); //*States that Loan Amount is $200,000, interest is 5.75% and it is 30 year term.
	System.out.println("\nThe Following is your output based on the hardcoded amount: \n"); //*Will show output in the following format
	print();
	System.exit(0); //*System exits nice and quietly
    }

    public void showAmortization()	{
	double pmt2interest = 0.0;  // payment-to-interest portion.
	double pmt2principl = 0.0;  // payment-to-principal portion.
	double pmtBalance = 0.0;

        System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance");
        System.out.println("====\t===========\t===========\t=======");

	for ( int i = 1; i <= months; ++i )
	{
	    pmt2interest = principal * rate;
	    pmt2principl = payment - pmt2interest;
	    pmtBalance = principal - pmt2principl;

 	do{
	 i++;
	} while(i<10000000);

	    System.out.println( "#" + i
			+ "\tP=" + currency.format(pmt2principl)
			+ "\tI=" + currency.format(pmt2interest)
			+ (pmt2interest < 10 ? "\t\tB=" : "\tB=")  // tab adjustment.
			+ currency.format(pmtBalance) );

	    principal -= pmt2principl; // reduce by our payment-to-principal
	}
    }
}

It looks like you took BestJewSinceJC's advice. If you take that do-while loop out, does it repeat the way you want? If so, I would imagine that it is pausing, yet pausing longer than you want it to. Try using a smaller number in the do-while loop.

This article has been dead for over six months. Start a new discussion instead.