1 Replies - 527 Views - Last Post: 20 April 2009 - 08:19 PM Rate Topic: -----

#1 steverh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 08

Variable may not have been initialized - Mortgage Calculator

Post icon  Posted 20 April 2009 - 07:48 PM

The program is supposed to request the loan amount, interest rate, and loan term and then give monthly payment amount and display the amortization table.
I was able to get the monthly payment but having a problem setting up my loop to print the table.

import java.io.*;
import java.text.*;

public class MortgageCalculatorWk3v2
{
	public static void main(String[] args) throws IOException
	{
		//declare and construct variables
		int LoanAmount, LoanTerm;
		double MonthlyPayment, InterestRate, MonthlyInterestPayment;

		System.out.println("Welcome to the Mortgage Calculator");
		System.out.println();

		LoanAmount = getLoanAmount();
		LoanTerm = getLoanTerm();
		InterestRate = getInterestRate();
		MonthlyPayment = getMonthlyPayment(LoanAmount, LoanTerm, InterestRate);
		output(MonthlyPayment);
		AmortizationTable(MonthlyInterestPayment, LoanAmount, MonthlyPayment);
		finish ();
	}

	//the getLoanAmount method retrieves the amount to be financed
	public static int getLoanAmount() throws IOException
	{
		String LnAmnt;
		int LoanAmount = 0;
		boolean done = false;

		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

		//loop while not done
		while (!done)
		{
			try
			{
				System.out.print("Enter the loan amount:");
				LnAmnt = dataIn.readLine();
				LoanAmount = Integer.parseInt(LnAmnt);
				if (LoanAmount <= 0) throw new NumberFormatException();
				else done = true;
			}
			catch(NumberFormatException e)
			{
				System.out.print("Please enter in loan amount in number format only.");
			}
		}
		return LoanAmount;
	}

	//the getLoanTerm method retrieves the length of the loan
	public static int getLoanTerm() throws IOException
	{
		String LnTrm;
		int LoanTerm = 0;
		boolean done = false;

		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

		//loop while not done
		while (!done)
		{
			try
			{
				System.out.print("Enter the loan term in years:");
				LnTrm = dataIn.readLine();
				LoanTerm = Integer.parseInt(LnTrm);
				if (LoanTerm <= 0) throw new NumberFormatException();
				else done = true;
			}
			catch(NumberFormatException e)
			{
				System.out.print("Please enter the loan term in number format only.");
			}
		}
		return LoanTerm;
	}

	//the getInterestRate method retrieves the interest rate
	public static double getInterestRate() throws IOException
	{
		String IntRt;
		double InterestRate = 0;
		boolean done = false;

		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

		//loop while not done
		while (!done)
		{
			try
			{
				System.out.print("Enter the Interest Rate:");
				IntRt = dataIn.readLine();
				InterestRate = Double.parseDouble(IntRt);
				if (InterestRate <= 0) throw new NumberFormatException();
				else done = true;
			}
			catch(NumberFormatException e)
			{
				System.out.print("Please enter the interest rate in number format only.");
			}
		}
		return InterestRate;
	}

	//the getMonthlyPayment method calculates the monthly mortgage payment
	public static double getMonthlyPayment(int LoanAmount, int LoanTerm, double InterestRate)
	{
		double MonthlyPayment;
		double EQ1;
		double EQ2;
		double MonthlyInterestRate;
		double LoanTerminMonths;


		//calculations
		MonthlyInterestRate = (InterestRate / 12) * .01;
		LoanTerminMonths = LoanTerm * 12;
		EQ1 = MonthlyInterestRate * Math.pow((1 + MonthlyInterestRate), LoanTerminMonths);
		EQ2 = Math.pow((1 + MonthlyInterestRate), LoanTerminMonths) -1;
		MonthlyPayment = LoanAmount * (EQ1 / EQ2);

		return MonthlyPayment;
	}

	//the output method displays the results of the calculation
	public static void output(double MonthlyPayment) throws IOException
	{
		DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
		System.out.println();
		System.out.println("\tThe monthly mortgage payment will be " + twoDigits.format(MonthlyPayment));

		//option to show amortization table
		String AmortTableRequest;
		int AmortTable = 0;

		System.out.println();
		System.out.println("Press 1 to see the loan amortization table");

		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

		AmortTableRequest = dataIn.readLine();
		AmortTable = Integer.parseInt(AmortTableRequest);
		if (AmortTable != 1) finish();
	}

	//the AmortizationTable method calculates and prints out payment details for each payment in the loan
	public static void AmortizationTable(double MonthlyInterestPayment, double LoanAmount, double MonthlyPayment)
	{
		double MonthlyInterestRate;
		double PrincipalPayment;
		double NewPrincipalBalance = 0.0;
		int LoanTerminMonths;
		int PymntNum = 1;

		String TableHeader = (("\nPymntNum") + ("\tStrtBlnc") + ("\tPrincPymnt") + ("\tIntPymnt") + ("\tEndBlnc"));
		System.out.println();
		System.out.print(TableHeader);
		DecimalFormat twoDigits = new DecimalFormat("$#,000.00");

		while (PymntNum != LoanTerminMonths)
		{
			MonthlyInterestPayment = LoanAmount * MonthlyInterestRate;
			PrincipalPayment = MonthlyPayment - MonthlyInterestPayment;
			NewPrincipalBalance = LoanAmount - PrincipalPayment;
			System.out.print((PymntNum) + "\t\t" + twoDigits.format(PrincipalPayment) + "\t\t" + twoDigits.format(MonthlyInterestPayment) + "\t\t" + twoDigits.format(NewPrincipalBalance));
		}

		PymntNum = (PymntNum + 1);
		LoanAmount = NewPrincipalBalance;
	}


		//Finish Method
		public static void finish()
	{
		System.exit(0);
	}

}



The errors that I am getting are:
variable MonthlyInterestPayment might not have been initialized
AmortizationTable(MonthlyInterestPayment, LoanAmount, MonthlyPayment);
variable LoanTerminMonths might not have been initialized
while (PymntNum != LoanTerminMonths)
variable MonthlyInterestRate might not have been initialized
MonthlyInterestPayment = LoanAmount * MonthlyInterestRate;

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Variable may not have been initialized - Mortgage Calculator

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Variable may not have been initialized - Mortgage Calculator

Posted 20 April 2009 - 08:19 PM

look at that code

while (PymntNum != LoanTerminMonths)
		{
			MonthlyInterestPayment = LoanAmount * MonthlyInterestRate


OK YOU know for sure that PymntNum != LoanTerminMonths will not be equal at least one time
but the Java compoiler is not so sure about it... actually it can know
so the Java compiler realize that you might not go into that loop
so it is afraid that MonthlyInterestPayment might not contains anything so it complains
doing

double MonthlyInterestPayment = 0.0;
will fix your problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1