• 0

[Java] Trouble getting a calculator to work


Question

I have a basic mortgage calculator and it works fine except for one thing. I'm supposed to display the monthly balance and interest paid for every month until it reaches zero. I have everything else working fine as far as I know I just can't seem to figure it out. I think I would have to use an incremental counter of sorts such as ++ in C++ but I'm not sure.

	import java.util.Date;
	import java.text.NumberFormat;
	import java.text.DecimalFormat;

	public class MortgageCalculator
		{
			public static void main(String[] args)
				{

				//Declare the variables
				int loanAmount = 200000;
				int loanTerm = 360;
				double interest = .0575;
				double monthlyPayment = 0;
				double loanBalance = 0;
				double interestPaid = 0;
				int lineCount = 10;
				NumberFormat currency = NumberFormat.getCurrencyInstance();

				//Display two decimal places
				java.text.DecimalFormat dec = new java.text.DecimalFormat("0.00");

				//Displays the mortgage calculator
				Date currentDate = new Date(); // Date constructor
				System.out.println();
				System.out.println("\tMortgage Calculator");
				System.out.println("\t" + currentDate);
				System.out.println();
		 		System.out.println("\tLoan amount is: $" + dec.format(loanAmount));
				System.out.println("\tInterest rate is: " + interest*100 + "%");
				System.out.println("\tLength of the loan is: " + loanTerm/12 + " years");
				System.out.println();

				//Declares the formula
				monthlyPayment = (loanAmount*(interest/12)) / (1 - 1 /Math.pow((1 + interest/12), loanTerm));
				System.out.println("\tThe monthly payment for month number is: " + "$" + dec.format(monthlyPayment));
				System.out.println();

				//Starts the loop statement and declares formula for loan balance and interest paid
				loanBalance = loanAmount - monthlyPayment;
				interestPaid = monthlyPayment * interest;
				while (loanBalance > 0){

				//Displays the loan balance and interest paid

				System.out.println("The loan balance is: $" + dec.format(loanBalance));
				System.out.println("The interest paid on the loan is: $" + dec.format(interestPaid));

				//Pauses screen
				if (lineCount == 10) {
				//lineCount = 0;
				try {
				Thread.sleep(4500);
				} catch (InterruptedException e) {
				}
			}
	}
				//Stops loop statement
				if (loanBalance == 0) {
				System.out.println("The loan balance is: $0.00");
				}

	}
}

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

Basically I need something like this:

Payment 1

Loan Balance=

Interest Paid=

Payment 2

Loan Balance=

Interest Paid=

I want it to display the payment and interest amounts for every month and the resulting balance until the balance reaches 0. I got it to display the first payment and interest amount I just need it to display the remaining payments, etc.

Link to comment
Share on other sites

  • 0

In that case I think you want to put the part of your code that calculates each month's values inside a while loop that has as its condition the fact that the balance is greater than 0

Link to comment
Share on other sites

  • 0

There is a while loop, but it displays the same balance and interest paid over and over. I need some type of incremental counter to show each payment as the balance decreases until the balance reaches zero.

//Starts the loop statement and declares formula for loan balance and interest paid
				loanBalance = loanAmount - monthlyPayment;
				interestPaid = monthlyPayment * interest;
				while (loanBalance > 0){

				//Displays the loan balance and interest paid

				System.out.println("The loan balance is: $" + dec.format(loanBalance));
				System.out.println("The interest paid on the loan is: $" + dec.format(interestPaid));

				//Pauses screen
				if (lineCount == 10) {
				//lineCount = 0;
				try {
				Thread.sleep(4500);
				} catch (InterruptedException e) {
				}
			}
	}
				//Stops loop statement
				if (loanBalance == 0) {
				System.out.println("The loan balance is: $0.00");
				}

Link to comment
Share on other sites

  • 0

I think the

				loanBalance = loanAmount - monthlyPayment;
				interestPaid = monthlyPayment * interest;

lines should be inside the while loop, assuming that you always use that formula

Link to comment
Share on other sites

  • 0

import java.util.Date;
import java.text.NumberFormat;
import java.text.DecimalFormat;

public class MortgageCalculator {
	public static void main(String[] args) {

		// Declare the variables
		int loanAmount = 200000;
		int loanTerm = 360;
		double interest = .0575;
		double monthlyPayment = 0;
		double loanBalance = 0;
		double interestPaid = 0;
		int lineCount = 10;
		NumberFormat currency = NumberFormat.getCurrencyInstance();

		// Display two decimal places
		java.text.DecimalFormat dec = new java.text.DecimalFormat("0.00");

		// Displays the mortgage calculator
		Date currentDate = new Date(); // Date constructor
		System.out.println();
		System.out.println("\tMortgage Calculator");
		System.out.println("\t" + currentDate);
		System.out.println();
		System.out.println("\tLoan amount is: $" + dec.format(loanAmount));
		System.out.println("\tInterest rate is: " + interest * 100 + "%");
		System.out.println("\tLength of the loan is: " + loanTerm / 12
				+ " years");
		System.out.println();

		// Declares the formula
		monthlyPayment = (loanAmount * (interest / 12))
				/ (1 - 1 / Math.pow((1 + interest / 12), loanTerm));
		System.out.println("\tThe monthly payment for month number is: " + "$"
				+ dec.format(monthlyPayment));
		System.out.println();

		// Starts the loop statement and declares formula for loan balance and
		// interest paid
		loanBalance = loanAmount - monthlyPayment;
		int paymentNumber = 1;
		while (loanBalance > 0) {
			interestPaid = monthlyPayment * interest;
			// Displays the loan balance and interest paid

			System.out.println("Payment " + paymentNumber
					+ ":\nThe loan balance is: $" + dec.format(loanBalance));
			System.out.println("The interest paid on the loan is: $"
					+ dec.format(interestPaid) + "\n");

			// Pauses screen
			if (lineCount == 10) {
				// lineCount = 0;
				try {
					Thread.sleep(45);
				} catch (InterruptedException e) {
				}
			}

			loanBalance = loanBalance - monthlyPayment;
			paymentNumber++;
		}
		// Stops loop statement
		if (loanBalance == 0) {
			System.out.println("The loan balance is: $0.00");
		}

	}
}

This might be what you are looking for, I think there might be an issue with the last payment's interest though.

Link to comment
Share on other sites

  • 0

Yes! That's exactly what I meant, the payment++ counter is what I was trying to get to work. I just need to get it to work for the interest as well since it's displaying the same interest amount for every payment but I should be able to get that worked out.

Link to comment
Share on other sites

  • 0

I've been working on this for a while now but I'm stuck at getting the correct output. Basically it's a loan amortization program so there must be a payment for every month of the designated loan period. I get it to display 359 payments but they all have the same payment amount and the sleep timer doesn't work either. Any help??

import java.util.Date;
import java.text.NumberFormat;
import java.text.DecimalFormat;

public class MortgageCalculator {
	public static void main(String[] args) {

		// Declare the variables
		double principle = 200000;
		int loanTerm = 360;
		double interest = 5.75;
		double monthlyPayment = 0;
		double interestPaid, payment, loanBalance;
		int lineCount = 0;
		int paymentNumber = 1;
		NumberFormat currency = NumberFormat.getCurrencyInstance();

		// Display two decimal places
		java.text.DecimalFormat dec = new java.text.DecimalFormat("0.00");

		// Displays the mortgage calculator
		Date currentDate = new Date(); // Date constructor
		System.out.println();
		System.out.println("\tMortgage Calculator");
		System.out.println("\t" + currentDate);
		System.out.println();
		System.out.println("\tLoan amount is: $" + dec.format(principle));
		System.out.println("\tInterest rate is: " + interest + "%");
		System.out.println("\tLength of the loan is: " + loanTerm / 12
				+ " years");
		System.out.println();

		// Declares the formula
		double monthlyInterest = (interest / 100)  / 12;


		// Starts the loop statement and declares formula for loan balance and interest paid
		//interestPaid = principle * monthlyInterest;
		//payment = monthlyPayment - interestPaid;
		//loanBalance = principle - payment;

		while (paymentNumber < 360) {

		interestPaid = principle * monthlyInterest;
		monthlyPayment = principle * (monthlyInterest / (1 - Math.pow(1 + monthlyInterest, - loanTerm)));
		payment = monthlyPayment - interestPaid;
		loanBalance = principle - payment;

			// Displays the loan balance and interest paid
			System.out.println("Payment " + paymentNumber
					+ ":\nThe loan balance is: $" + dec.format(loanBalance));
			System.out.println("The interest paid on the loan is: $"
					+ dec.format(interestPaid) + "\n");

			// Pauses screen
			while (lineCount == 5){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
			}

			paymentNumber++;
		}
		// Stops loop statement
		if (paymentNumber == 360); {
			System.out.println("The loan balance is: $0.00");
		}

	}
}

Link to comment
Share on other sites

  • 0

The Sleep is not working because it is inside a while that says that lineCount must be equal to 5 and it's initialized at 0 and never changed.

For the other thing, we'd need to know the mathematical formula you want for calculating the values.

Also, try indenting your code correctly for legibility. Are you using an IDE, if so, which one? Also, have you used the debugger?

Link to comment
Share on other sites

  • 0

Alright I got the formula figured out and got the program working properly. Now I'm modifying the program to use an array in order to do all of this for three interest rates and loan terms. For now I have one small problem. It is set to stop and ask the user to hit Enter after every set of data, but it only stops after the first set and last set. For some reason it doesn't stop for the second set of data. Any idea why?

import java.util.Date;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.*;

public class MortgageCalculator {
	public static void main(String[] args) throws IOException{

		// Declare the variables
		double loanAmt = 200000;
		int loanTerm = 360;
		double interest = 5.75;
		double payment1, payment2, payment3; // Creates three payment variables
		double interestPaid, loanBalance, monthlyPayment;
		double months = 360;
		int paymentNumber = 1;
		int i, j;
		NumberFormat currency = NumberFormat.getCurrencyInstance();

		//Create arrays

		double [] Term = {84, 180, 360};
		double [] Rate = {.0535, .0550, .0575};

		// Display two decimal places
		java.text.DecimalFormat dec = new java.text.DecimalFormat("0.00");

		for (i = 0; i <= 2; i++){

			for (j = 0; j<= Term[i]; j++){

		// Calculates monthly payment and interest
		monthlyPayment = (loanAmt * (Rate[i]/12)) / (1 - Math.pow((1 + Rate[i]/12), - Term[i]));


		// Displays the mortgage calculator
			Date currentDate = new Date(); // Date constructor
			System.out.println();
			System.out.println("\tMortgage Calculator");
			System.out.println("\t" + currentDate);
			System.out.println();
			System.out.println("\tLoan amount is: $" + dec.format(loanAmt));
			System.out.println("\tInterest rate is: " + Rate[i] * 100 + "%");
			System.out.println("\tLength of the loan is: " + Term[i] / 12
						+ " years");
			System.out.println("\tThe monthly payment is: $" + dec.format(monthlyPayment));
			System.out.println();
			System.out.println("Press Enter to Continue.");
			System.in.read ();


		// Starts the loop statement and declares formula for loan balance and interest paid
		while (Term[i] > 0) {

			months = (Term[i] --);

			/*interestPaid = loanAmt * Rate[i];
			payment1 = monthlyPayment - interestPaid;
			loanBalance = loanAmt - payment1;
			loanAmt = loanBalance;

			// Displays the loan balance and interest paid
			System.out.println("Payment " + paymentNumber
					+ ":\nThe loan balance is: $" + dec.format(loanBalance));
			System.out.println("The interest paid on the loan is: $"
					+ dec.format(interestPaid) + "\n");
			*/

			// Pauses loop
			if (paymentNumber % 10 == 0){
				System.out.println("Press ENTER to continue ");
				System.in.read();
			}

		}
	}
}

		// Stops loop statement
		if (paymentNumber == 360); {
			System.out.println();
			System.out.println("The loan balance is: $0.00");
			System.out.println();
		}

	}
}

Link to comment
Share on other sites

  • 0

your condition seems very weird, I'd just put this:

				System.out.println("Press ENTER to continue ");
				System.in.read();

at the end of the outer loop without a condition

Link to comment
Share on other sites

  • 0

If I block out all of the stops except the one right below the lines which display the information it does the same thing. It stops when it displays the first set of data, but it shows the next two sets at the same time without stopping.I tried placing the stop outside of the various loops but it made it worse.

import java.util.Date;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.*;

public class MortgageCalculator {
	public static void main(String[] args) throws IOException{


		// Declare the variables
		double loanAmt = 200000;
		double loanTerm = 360;
		double interest = 5.75;
		double payment1, payment2, payment3; // Creates three payment variables
		double interestPaid, loanBalance, monthlyPayment;
		double months = 360;
		int paymentNumber = 1;
		int i, j;
		NumberFormat currency = NumberFormat.getCurrencyInstance();


		//Create arrays
		double [] Term = {84, 180, 360};
		double [] Rate = {.0535, .0550, .0575};


		// Display two decimal places
		java.text.DecimalFormat dec = new java.text.DecimalFormat("0.00");

		for (i = 0; i <= 2; i++){

			for (j = 0; j<= Term[i]; j++){


		// Calculates monthly payment and interest
		monthlyPayment = (loanAmt * (Rate[i]/12)) / (1 - Math.pow((1 + Rate[i]/12), - Term[i]));


		// Displays the mortgage calculator
			Date currentDate = new Date(); // Date constructor
			System.out.println();
			System.out.println("\tMortgage Calculator");
			System.out.println("\t" + currentDate);
			System.out.println();
			System.out.println("\tLoan amount is: $" + dec.format(loanAmt));
			System.out.println("\tInterest rate is: " + Rate[i] * 100 + "%");
			System.out.println("\tLength of the loan is: " + Term[i] / 12
						+ " years");
			System.out.println("\tThe monthly payment is: $" + dec.format(monthlyPayment));
			System.out.println();
			System.out.println("Press Enter to Continue.");
			System.in.read ();


		// Starts the loop statement and declares formula for loan balance and interest paid
		/*while (Term[i] > 0) {

			months = (Term[i] --);

			interestPaid = loanAmt * Rate[i];
			payment1 = monthlyPayment - interestPaid;
			loanBalance = loanAmt - payment1;
			loanAmt = loanBalance;

			// Displays the loan balance and interest paid
			System.out.println("Payment " + paymentNumber
					+ ":\nThe loan balance is: $" + dec.format(loanBalance));
			System.out.println("The interest paid on the loan is: $"
					+ dec.format(interestPaid) + "\n");
			*/


			// Pauses loop
			/*if (paymentNumber % 10 == 0){
				System.out.println("Press ENTER to continue ");
				System.in.read();
			}

		}*/
	}

}


		// Stops loop statement
		/*if (paymentNumber == 360); {
			System.out.println();
			System.out.println("The loan balance is: $0.00");
			System.out.println();
		}*/

	}
}

Link to comment
Share on other sites

  • 0

I have fixed the enter issue, however your algorithm seems to have issues since the results don't make sense, you have to fix that first before worrying about enters and such.

Unfortunately I don't understand your math or that of the page you linked to (WTF is a negative power, power of what?) so I can't help you there.

Also, please try to keep your code correctly indented when posting since that helps readability a lot.

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

public class MortgageCalculator {
	public static void main(String[] args) throws IOException{

		// Declare the variables
		double loanAmt = 200000;
		double payment; // Creates a payment variable
		double interestPaid = 0, loanBalance, monthlyPayment;
		int i, j;

		//Create arrays
		double [] Term = {84, 180, 360};
		double [] Rate = {.0535, .0550, .0575};

		// Display two decimal places
		DecimalFormat dec = new DecimalFormat("0.00");

		for (i = 0; i < 3; i++) {
			int paymentNumber = 1;
			for (j = 0; j < Term[i]; j++) {
				// Calculates monthly payment and interest
				monthlyPayment = (loanAmt * (Rate[i]/12)) / (1 - Math.pow((1 + Rate[i]/12), - Term[i]));

				// Displays the mortgage calculator
				Date currentDate = new Date(); // Date constructor
				System.out.println("\n\tMortgage Calculator");
				System.out.println("\t" + currentDate);
				System.out.println("\n\tLoan amount is: $" + dec.format(loanAmt));
				System.out.println("\tInterest rate is: " + Rate[i] * 100 + "%");
				System.out.println("\tLength of the loan is: " + Term[i] / 12
						+ " years");
				System.out.println("\tThe monthly payment is: $" + dec.format(monthlyPayment));
				System.out.println("\nPress Enter to Continue.");
				System.in.read ();


				// Starts the loop statement and declares formula for loan balance and interest paid
				while (Term[i] > 0) {
					Term[i]--;

					interestPaid = loanAmt * Rate[i];
					payment = monthlyPayment - interestPaid;
					loanBalance = loanAmt - payment;
					loanAmt = loanBalance;

					// Displays the loan balance and interest paid
					System.out.println("Payment " + paymentNumber
							+ ":\nThe loan balance is: $" + dec.format(loanBalance));
					System.out.println("The interest paid on the loan is: $"
							+ dec.format(interestPaid) + "\n");
					paymentNumber++;
				}
			}
			// Pauses loop
			System.out.println("Press ENTER to continue");
			System.in.read();
		}
		System.out.println("\nThe loan balance is: $0.00\n");
	}
}

Link to comment
Share on other sites

  • 0

I know the results are way off that's why I commented the formulas out. I just need the array to work by with displaying the monthly payment for the three interest rates. The rest of the program I'll figure out the next few days.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.