Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Mortgage calulator HW problem

1. ## Mortgage calulator HW problem

Hello,

I am very new to programming and I have hit a stuck point in my code. This is for a mortgage calculator and we have to use the math.pow method. I am getting results, but the results are wrong. The monthly payment is coming out to big.

Here is what it should be:

Enter loan amount: 10000
Enter rate: 4.5
Enter number years: 3

The monthly payment is: \$297.47

Here is what I'm getting.

Enter loan amount: 10000
Enter rate: 4.5
Enter number years: 3

The monthly payment is: \$566.06

Here is my code. I believe my issue is in the math.pow formula. Can someone help my out?

Scanner in = new Scanner(System.in); // input object

double loanAmount; //user input
double interestRate; // user input
double yearNumber; // user input
double monthlyPayment; // answer to math
double finalPayment; // output
double monthNumber; // year number converted of months
double monthlyRate; // converted intersted rate

System.out.print("Enter loan amount: "); // Shows Line to input loan amount
loanAmount = Double.parseDouble (in.nextLine()); // gets input number

System.out.print("Enter rate: "); // Shows line to input rate amount
interestRate = Double.parseDouble (in.nextLine()); // gets input number

monthlyRate = interestRate * .01; //convert rate into decimal

System.out.print("Enter number years: "); //Shows line to input number of years
yearNumber = Double.parseDouble (in.nextLine()); // gets input number

monthNumber = yearNumber * 12; // total amount of months

monthlyPayment = Math.pow(1+ interestRate,monthNumber) / (Math.pow(1+interestRate, monthNumber)-1);

finalPayment = (loanAmount*monthlyRate*monthlyPayment); // gives the payment

System.out.printf("\nThe monthly payment is: \$%.2f", monthlyPayment); // Shows monthly payment  Reply With Quote

3. ## Re: Mortgage calulator HW problem

Please use highlight tags when posting code to preserve formatting. Also, please post your code as an MCVE so that we can copy and paste it to run it. I currently don't see a main method or a class definition.

What happened when you stepped through this with a debugger, or at least added some print statements, to figure out when the program's execution differed from what you expected? What exactly do you mean when you say the output is wrong? Wrong in what way?  Reply With Quote

4. ## Re: Mortgage calulator HW problem

I'm sorry. I don't understand how to use highlight tags. But the answer should be \$297.47 in this example and I am getting \$566.06. My answer is incorrect meaning the math must be wrong. I can't figure out where it is wrong.

--- Update ---

Here is the entire code.

package assignment2;

import java.util.Scanner;

/**
*
*
*/
public class Assignment2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Scanner in = new Scanner(System.in); // input object

double loanAmount; //user input
double interestRate; // user input
double yearNumber; // user input
double monthlyPayment; // answer to math
double finalPayment; // output
double monthNumber; // year number converted of months
double monthlyRate; // converted intersted rate

System.out.print("Enter loan amount: "); // Shows Line to input loan amount
loanAmount = Double.parseDouble (in.nextLine()); // gets input number

System.out.print("Enter rate: "); // Shows line to input rate amount
interestRate = Double.parseDouble (in.nextLine()); // gets input number

monthlyRate = interestRate * .01; //convert rate into decimal

System.out.print("Enter number years: "); //Shows line to input number of years
yearNumber = Double.parseDouble (in.nextLine()); // gets input number

monthNumber = yearNumber * 12; // total amount of months

monthlyPayment = Math.pow(1+ interestRate,monthNumber) / (Math.pow(1+interestRate, monthNumber)-1);

finalPayment = (loanAmount*monthlyRate*monthlyPayment); // gives the payment

System.out.printf("\nThe monthly payment is: \$%.2f", monthlyPayment); // Shows monthly payment
System.out.println();

}

}  Reply With Quote

5. ## Re: Mortgage calulator HW problem

convert your Annual interest rate into monthly interest rate. Your annual interest rate here is 4.5 which will become 4.5/100=0.045, 0.045/12=0.00375.  Reply With Quote

6. ## Re: Mortgage calulator HW problem

I convert the annual interest rate with
monthlyRate = interestRate * .01; //convert rate into decimal

Will that not do the same as /100?  Reply With Quote

7. ## Re: Mortgage calulator HW problem

Add some println statements to print out the parts of the expression that the code uses to see which one is not doing what you expect.  Reply With Quote

8. ## Re: Mortgage calulator HW problem  Reply With Quote

9. ## Re: Mortgage calulator HW problem

Do you have an example?  Reply With Quote

10. ## Re: Mortgage calulator HW problem

There are 12 months in a year.

If you eat 120 apples per year, that means you eat 10 apples per month.

What is your interest per year? Then what is your interest per month?  Reply With Quote

11. ## Re: Mortgage calulator HW problem Originally Posted by Rods I read this before and it helped. I missed the part about monthly rate. so I just added this
yearRate = interestRate * .01; //convert rate into decimals
** monthlyRate = yearRate / 12; // convert rate to month rate

And now it works correctly. Thanks for your help everyone.  Reply With Quote

12. ## Re: Mortgage calulator HW problem

Excellent. Enjoy.   Reply With Quote