Java console mortgage calculator

Java console mortgage calculator

Page 1 of 1

8 Replies - 2945 Views - Last Post: 08 March 2009 - 08:41 PM Rate Topic: -----

#1 onecpudr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Java console mortgage calculator

Posted 08 March 2009 - 05:53 PM

Hi. I'm trying to create a mortgage console program, but it is not calculating for me. can someone please help??


import java.io.*;
public class MortgagePayment3
{

	public static void main(String[] args) throws IOException
	{
		String principal,annualInterest,years;  //*Principle amount of loan
		int annRate;
			BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter Principal Amount ");
				principal = dataIn.readLine();
			System.out.println("Enter term of the loan, ex. 30 years ");
			 	years =  dataIn.readLine();
			System.out.println("\nEnter your interest rate"); //*Interest rate of loan
				annualInterest = dataIn.readLine();
				annRate = Integer.parseInt(annualInterest);
			System.out.println("\nThe Following is your output based on the amount: \n"); //*Will show output in the following format
}
	public static double calculatePayment(double principal, double annRate, int years){
		double monthlyInt = annRate / 12;
		double monthlyPayment = (principal * monthlyInt)
					/ (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Shows 1 monthly payment multiplied by 12 to make one complete year.
		return format(monthlyPayment, 2);
	}
	public static double format(double amount, int mortgage) {
		double temp = amount;
		temp = temp * Math.pow(10, mortgage);
		temp = Math.round(temp);
		temp = temp/Math.pow(10, mortgage);
		return temp;
	}
	public static void print(double pr, double annRate, int years){
		double mpayment = calculatePayment(pr, annRate, years);
		System.out.println("The principal is $" + (int)pr); //*Shows the principle amount in $ value.
		System.out.println("The annual interest rate is " + format(annRate * 100, 2) +"%");
		System.out.println("The term is " + years + " years"); //*Term is normally in years.
		System.out.println("Your monthly payment is $" + mpayment); //*Shows output of monthly payment.
		System.exit(0); //*System exits nice and quietly


	}
}

This post has been edited by onecpudr: 08 March 2009 - 06:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java console mortgage calculator

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java console mortgage calculator

Posted 08 March 2009 - 05:57 PM

Welcome!

Please edit your post above to add the code tags:

:code:

When you edit your post, please notice the CODE button. When you click it, it will insert the the tags for you. The first click inserts the opening tag, and the second click will insert the closing tag.

Also please describe what your problem is.

Thanks!

This post has been edited by n8wxs: 08 March 2009 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java console mortgage calculator

Posted 08 March 2009 - 06:30 PM

View Postonecpudr, on 8 Mar, 2009 - 04:53 PM, said:

Hi. I'm trying to create a mortgage console program, but it is not calculating for me. can someone please help??

You have declared annRate as an integer. You should use a double instead because the interest rate is likely to be fractional - 5.75% for example. Using a double, you'll need to change
annRate = Integer.parseInt(annualInterest);
to
annRate = Double.parseDouble(annualInterest);

This post has been edited by n8wxs: 08 March 2009 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 onecpudr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Re: Java console mortgage calculator

Posted 08 March 2009 - 06:36 PM

View Postn8wxs, on 8 Mar, 2009 - 05:30 PM, said:

View Postonecpudr, on 8 Mar, 2009 - 04:53 PM, said:

Hi. I'm trying to create a mortgage console program, but it is not calculating for me. can someone please help??

You have declared annRate as an integer. You should use a double instead because the interest rate is likely to be fractional - 5.75% for example. Using a double, you'll need to change
annRate = Integer.parseInt(annualInterest);
to
annRate = Double.parseDouble(annualInterest);



Quote

Thank you. I made that change but now I am getting the following error:

C:\Documents and Settings\Administrator\My Documents\UOP\PRG420\MortgagePayment3.java:20: possible loss of precision
found : double
required: int
annRate = Double.parseDouble(annualInterest);
^
1 error

Tool completed with exit code 1

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: Java console mortgage calculator

Posted 08 March 2009 - 07:29 PM

   public static void main(String[] args) throws IOException
	{
		String principal,annualInterest,years;  //*Principle amount of loan
		int annRate;
			BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter Principal Amount ");
				principal = dataIn.readLine();
			System.out.println("Enter term of the loan, ex. 30 years ");
				 years =  dataIn.readLine();
			System.out.println("\nEnter your interest rate"); //*Interest rate of loan
				annualInterest = dataIn.readLine();
				annRate = Integer.parseInt(annualInterest);
			System.out.println("\nThe Following is your output based on the amount: \n");
}


You prompt the user for input... values that you read
but you never call your calculatePayment() method
Was This Post Helpful? 0
  • +
  • -

#6 onecpudr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Re: Java console mortgage calculator

Posted 08 March 2009 - 07:51 PM

View Postpbl, on 8 Mar, 2009 - 06:29 PM, said:

You prompt the user for input... values that you read
but you never call your calculatePayment() method


Sure I do

double mpayment = calculatePayment(pr, annRate, years);

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: Java console mortgage calculator

Posted 08 March 2009 - 07:55 PM

View Postonecpudr, on 8 Mar, 2009 - 06:51 PM, said:

Sure I do

double mpayment = calculatePayment(pr, annRate, years);

This is in your print() method
who calls your print() method ?
Was This Post Helpful? 0
  • +
  • -

#8 onecpudr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-March 09

Re: Java console mortgage calculator

Posted 08 March 2009 - 08:19 PM

View Postpbl, on 8 Mar, 2009 - 06:55 PM, said:

This is in your print() method
who calls your print() method ?


OK thats why I'm here asking. I don't know. I thought I had apparently not. Can you help me with how I should do that? Here is what is in the code right now:

System.out.println("Your monthly payment is $" + mpayment); //*Shows output of monthly payment.



I thought this was the print of the payment amount but it is not working.

This post has been edited by onecpudr: 08 March 2009 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Java console mortgage calculator

Posted 08 March 2009 - 08:41 PM

Not clear when you use int or double

something like this ?

		String principal,annualInterest,years;  //*Principle amount of loan
		double annRate;
		BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter Principal Amount ");
		principal = dataIn.readLine();
		System.out.println("Enter term of the loan, ex. 30 years ");
		years =  dataIn.readLine();
		System.out.println("\nEnter your interest rate"); //*Interest rate of loan
		annualInterest = dataIn.readLine();
		annRate = Integer.parseInt(annualInterest);
		System.out.println("\nThe Following is your output based on the amount: \n"); //*Will show output in the following format
		
		// convert fo parameters as print expects them
		int iYears = Integer.parseInt(years);
		double pr = Double.parseDouble(principal);
		double percent = Double.parseDouble(annualInterest);
		print(pr, percent, iYears);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1