Mortgage Calculator

I need help with Java Mortgage Calculator

Page 1 of 1

3 Replies - 1989 Views - Last Post: 15 June 2009 - 08:21 PM Rate Topic: -----

#1 jtown2046   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-June 09

Mortgage Calculator

Post icon  Posted 15 June 2009 - 12:25 AM

I am trying to modify the mortgage that will allow the user to enter 3 loan terms for the same amount financed. Use an array(s) to store the loans. Then print out a comparison of the payment amount, the total interest paid over the course of the loan, and total of payments.
It will compile but when I run it, it get an error after I enter inerest rate..Please help............

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

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


 public class Mortgage {

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

//Set the variables
  double amount;
  int[]  years = new int[3];
  String  LoanTerm;
  double interest;
  double monthly_interest_rate;
  double monthly_payment;
  double months;
  double balance;
  double paidInterest;
  double paidPrinciple;
  int payment=1;

   BufferedReader user_input= new BufferedReader(new InputStreamReader(System.in));
		DecimalFormat cur = new DecimalFormat("0.00");

// Output for user data

		System.out.print("Please Enter Amount: $");
			amount = Double.parseDouble(user_input.readLine());
		System.out.println();


int i = 0;
		for (i = 0; i < 3; i++)
	{
		System.out.print("Enter loan term " + (i+1) + " in years:");
			LoanTerm = user_input.readLine();
			years[i] = Integer.parseInt(LoanTerm);
	}
		System.out.println();

		System.out.print("Please Enter Interest Rate: ");
					interest = Double.parseDouble(user_input.readLine());
		System.out.println();


//Calculation for Monthly Payment


	months = (years[i]*12);

	monthly_payment = (amount *((interest/100/12) / (1 - Math.pow(1/(1 + interest/100/12), months))));


// The Monthly Payment output
		System.out.print("The Monthly Payment is: $");
		System.out.print(cur.format(monthly_payment));
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("Press Enter to Continue");
		System.out.println();
		System.out.println();

// Pause program and wait user to press enter
   		System.in.read();
		System.out.print ("\t\t\t\tAMORTIZATION SCHEDULE");
		System.out.println ();
		System.out.println ();
		System.out.format ("No.\t  Monthly Payment  Interest Paid	Principle Paid\tBalance%n");
		System.out.print  ("---\t  ---------------  -------------	--------------\t-------");

// Balance equal to amount
	balance=amount;

//Loop for Principal, Interest and balance
			for (int num=1; num<=years[i]; num++)
{
//Calculations for Interest paid
	paidInterest = (balance * ((interest *.01)/12));

//Calculations for Principle Paid
	paidPrinciple = (monthly_payment - paidInterest);

//Calculations for Balance
	balance = (balance - paidPrinciple);

//Output for loop
		System.out.println();
		System.out.format  ("%d",payment++);
		System.out.format  ("\t\t" + cur.format(monthly_payment));
		System.out.format  ("\t\t" + cur.format(paidPrinciple));
		System.out.format  ("\t\t" + cur.format(paidInterest));
		System.out.format  ("\t\t" + cur.format(balance));
		System.out.print ("");

// Display 20 lines, pauses program and wait user to press enter
			if ((num)%20==0)
	{

		System.out.println();
		System.out.println();
		System.out.println("Press Enter to Continue");
		System.out.println();
		System.in.read();
		System.out.print ("\t\t\t\tAMORTIZATION SCHEDULE");
		System.out.println ();
 		System.out.println ();
		System.out.format ("No.\t  Monthly Payment  Interest Paid	Principle Paid\tBalance%n");
		System.out.print  ("---\t  ---------------  -------------	--------------\t-------		 ");
}
}
}
}
}

This post has been edited by jtown2046: 15 June 2009 - 01:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Mortgage Calculator

#2 pbl   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Mortgage Calculator

Posted 15 June 2009 - 06:31 PM

int i = 0;
		for (i = 0; i < 3; i++)
	{
		System.out.print("Enter loan term " + (i+1) + " in years:");
			LoanTerm = user_input.readLine();
			years[i] = Integer.parseInt(LoanTerm);
	}

// when you exit de loop here....  i will contain 4


//Calculation for Monthly Payment


	months = (years[i]*12);	 <------ and yeras[4] does not exist



I guess you need another loop for(i = 0; i < 3; i++) to loop through all your years
Was This Post Helpful? 0
  • +
  • -

#3 jtown2046   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-June 09

Re: Mortgage Calculator

Posted 15 June 2009 - 08:16 PM

View Postpbl, on 15 Jun, 2009 - 05:31 PM, said:

int i = 0;
		for (i = 0; i < 3; i++)
	{
		System.out.print("Enter loan term " + (i+1) + " in years:");
			LoanTerm = user_input.readLine();
			years[i] = Integer.parseInt(LoanTerm);
	}

// when you exit de loop here....  i will contain 4


//Calculation for Monthly Payment


	months = (years[i]*12);	 <------ and yeras[4] does not exist



I guess you need another loop for(i = 0; i < 3; i++) to loop through all your years



I don't know how to make it loop through the years
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Mortgage Calculator

Posted 15 June 2009 - 08:21 PM

	for(i = 0; i < 3; i++) {
	   months = years[i] * 12;

.... and all your calculations here

	}  <--- end of the for loop


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1