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");
}
}
}
Question
Crackler
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