change that to:

double balance = 0.0;

Solved

Posted on 2006-04-18

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++;

}

}

}

}

21 Comments

.'

/*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?!?!?

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

zeroMAx challenge | 20 | 66 | |

Enhanced For loop vs ForEach Loop | 4 | 42 | |

System.currentTimeMillis() |
15 | 43 | |

where is session ID cache stored | 1 | 26 |

The viewer will learn how to implement Singleton Design Pattern in Java.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**14** Experts available now in Live!