# Java console mortgage calculator

Page 1 of 1

## 8 Replies - 3273 Views - Last Post: 08 March 2009 - 08:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=91459&amp;s=ce4559b46ca3461a30919a72635775bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 onecpudr

Reputation: 0
• 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;
System.out.println("Enter Principal Amount ");
System.out.println("Enter term of the loan, ex. 30 years ");
System.out.println("\nEnter your interest rate"); //*Interest rate of loan
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

• --... ...-- -.. . -. ---.. .-- -..- ...

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

## Re: Java console mortgage calculator

Posted 08 March 2009 - 05:57 PM

Welcome!

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.

Thanks!

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

### #3 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

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

## Re: Java console mortgage calculator

Posted 08 March 2009 - 06:30 PM

onecpudr, 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

### #4 onecpudr

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

## Re: Java console mortgage calculator

Posted 08 March 2009 - 06:36 PM

n8wxs, on 8 Mar, 2009 - 05:30 PM, said:

onecpudr, 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

### #5 pbl

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

Reputation: 8362
• Posts: 31,955
• 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;
System.out.println("Enter Principal Amount ");
System.out.println("Enter term of the loan, ex. 30 years ");
System.out.println("\nEnter your interest rate"); //*Interest rate of loan
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

### #6 onecpudr

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

## Re: Java console mortgage calculator

Posted 08 March 2009 - 07:51 PM

pbl, 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);
```

### #7 pbl

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

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java console mortgage calculator

Posted 08 March 2009 - 07:55 PM

onecpudr, 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 ?

### #8 onecpudr

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

## Re: Java console mortgage calculator

Posted 08 March 2009 - 08:19 PM

pbl, 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

### #9 pbl

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

Reputation: 8362
• Posts: 31,955
• 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;
System.out.println("Enter Principal Amount ");
System.out.println("Enter term of the loan, ex. 30 years ");
System.out.println("\nEnter your interest rate"); //*Interest rate of loan
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);

```