This course will introduce you to C++ 11 and teach you about syntax fundamentals.

I am trying to print the payment schedule for my mortgage program. I have the variable balance declared at the top, however the compiler is saying 'MortgageCalculatorDisplay.java:46: variable balance might not have been initialized'. Do i have something else wrong in the code? thanks

/*class MortgageCalculator*/

import java.io.*;

import java.text.DecimalFormat;

class MortgageCalculatorDisplayT

{

public static void main(String arguments[])

{

/*Declaring Program Variables*/

double I = 0.0575;//*Interest rate

double P = 200000.00;//*Amount of Loant

double J = I/12; //*Monthy interest

int N = 360; //* 30 yrs -12 months each year is 360 months

double balance;

double monthlyInterest;

double monthlyPrincipal;

DecimalFormat df = new DecimalFormat("$#,###.00");

// Create new Decimal Format object that has the format required for this program

//calculate monthly payment

//Mortgage Formula for code is M=P*(J/(1-(1+j)**-N))

//P= principal amount of loan

//J= monthly interest in decimal form

//N= number of months

double mortgage = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));

double payment = P / mortgage; //*Rate Calculation

/*Output*/

//System.out.println (" The monthly mortgage payment is $" + df.format(payment));

// format column headers for results to be displayed

System.out.println("\n\nPayment\tPrincipal\t\tInterest\t\tBalance");

System.out.println("---------\t---------\t---------\t---------");

int lines = 0;

for(int i=0; i<N; i++)

{

// information to display

System.out.println((i+1) + "\t\t" + df.format(P) +

"\t\t" + df.format(J) +

"\t\t" + df.format(balance));

// calculate interest and principal payments

monthlyInterest = (balance * J);

monthlyPrincipal = (payment - monthlyInterest);

//reduce balance

balance -= monthlyPrincipal;

// pause every 25 lines

if(lines == 25)

{

lines = 0;

try

{

Thread.sleep(1000);

}

catch (InterruptedException e)

{

}

}

else

{

lines++;

}

}

}

}

/*class MortgageCalculator*/

import java.io.*;

import java.text.DecimalFormat;

class MortgageCalculatorDisplayT

{

public static void main(String arguments[])

{

/*Declaring Program Variables*/

double I = 0.0575;//*Interest rate

double P = 200000.00;//*Amount of Loant

double J = I/12; //*Monthy interest

int N = 360; //* 30 yrs -12 months each year is 360 months

double balance;

double monthlyInterest;

double monthlyPrincipal;

DecimalFormat df = new DecimalFormat("$#,###.00")

// Create new Decimal Format object that has the format required for this program

//calculate monthly payment

//Mortgage Formula for code is M=P*(J/(1-(1+j)**-N))

//P= principal amount of loan

//J= monthly interest in decimal form

//N= number of months

double mortgage = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));

double payment = P / mortgage; //*Rate Calculation

/*Output*/

//System.out.println (" The monthly mortgage payment is $" + df.format(payment));

// format column headers for results to be displayed

System.out.println("\n\nPa

System.out.println("------

int lines = 0;

for(int i=0; i<N; i++)

{

// information to display

System.out.println((i+1) + "\t\t" + df.format(P) +

"\t\t" + df.format(J) +

"\t\t" + df.format(balance));

// calculate interest and principal payments

monthlyInterest = (balance * J);

monthlyPrincipal = (payment - monthlyInterest);

//reduce balance

balance -= monthlyPrincipal;

// pause every 25 lines

if(lines == 25)

{

lines = 0;

try

{

Thread.sleep(1000);

}

catch (InterruptedException e)

{

}

}

else

{

lines++;

}

}

}

}

.'

/*class MortgageCalculator*/

import java.io.*;

import java.text.DecimalFormat;

class MortgageCalculatorDisplayT

{

public static void main(String arguments[])

{

/*Declaring Program Variables*/

double I = 0.0575;//*Interest rate

double P = 200000.00;//*Amount of Loant

double J = I/12; //*Monthy interest

int N = 360; //* 30 yrs -12 months each year is 360 months

double balance = 0.0;

double monthlyInterest;

double monthlyPrincipal;

DecimalFormat df = new DecimalFormat("$#,###.00")

// Create new Decimal Format object that has the format required for this program

//calculate monthly payment

//Mortgage Formula for code is M=P*(J/(1-(1+j)**-N))

//P= principal amount of loan

//J= monthly interest in decimal form

//N= number of months

double mortgage = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));

double payment = P / mortgage; //*Rate Calculation

/*Output*/

//System.out.println (" The monthly mortgage payment is $" + df.format(payment));

// format column headers for results to be displayed

System.out.println("\n\nPa

System.out.println("------

int lines = 0;

for(int i=0; i<N; i++)

{

// information to display

System.out.println((i+1) + "\t\t" + df.format(P) +

"\t\t" + df.format(J) +

"\t\t" + df.format(balance));

// calculate interest and principal payments

monthlyInterest = (balance * J);

monthlyPrincipal = (payment - monthlyInterest);

//reduce balance

balance -= monthlyPrincipal;

// pause every 25 lines

if(lines == 25)

{

lines = 0;

try

{

Thread.sleep(1000);

}

catch (InterruptedException e)

{

}

}

else

{

lines++;

}

}

}

}

java.lang.NoClassDefFoundE

Exception in thread "main"

Do i need to import a different class? Thx

i have this to calculate, do i need to change that interest??

// calculate interest and principal payments

monthlyInterest = (balance * J);

monthlyPrincipal = (payment - monthlyInterest);

//reduce balance

balance -= monthlyPrincipal;

>> double J = I/12.0; //*Monthy interest

Won't help as I is a double already, which means that the int 12 is promoted to a double.

The calculation is not working correctly. Is this for a fixed monthly payment type of mortgage?

public static void main(String arguments[]) {

/*Declaring Program Variables*/

double annualInterestRate = 0.0575;//*Interest rate

double principal = 200000.00;//*Amount of Loant

double monthlyInterestRate = annualInterestRate/12; //*Monthy interest

int periods = 360; //* 30 yrs -12 months each year is 360 months

double balance = principal;

double monthlyInterest;

double monthlyPrincipal;

DecimalFormat df = new DecimalFormat("$#,###.00")

System.out.println("\n\nPa

System.out.println("------

int lines = 0;

for(int i=0; i < periods; i++) {

double interestPayment = balance * monthlyInterestRate;

double amortization = principal / periods;

double payment = interestPayment + amortization;

// information to display

System.out.println((i+1) + "\t\t\t" + df.format(balance) +

"\t\t" + df.format(interestPayment)

"\t\t" + df.format(amortization) +

"\t\t" + df.format(payment));

//reduce balance

balance -= amortization;

// pause every 25 lines

if(lines == 25) {

lines = 0;

try { Thread.sleep(1000); } catch (InterruptedException e) {}

} else {

lines++;

}

}

}

Eerrh, so you take my solution and give objects the credit (and points) for something that doesn't work?!?!?

I am sorry, I am new to using this site. I have only had a few posts. I did not know that you could select which one to grade, i thought it was the entire post I responded to. I am so sorry. If they can re-open, i can adjust.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

change that to:

double balance = 0.0;