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


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Monthly mortgage payment calculator

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Monthly mortgage payment calculator

    IM writing a monthly mortgage payment calculator I have the formula but dont know how to enter it:

    the formula i have is

    pmt = ((p-d)*r/12) / (1-(1+(r/12)^-m)

    my code is:
    import java.util.Scanner;

    public class Project1 {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the price of the house: ");
    double housePrice = input.nextDouble();
    System.out.println("Enter the down payment: ");
    double downPayment = input.nextDouble();
    System.out.println("Enter the annual interest rate: ");
    double annualInterestRate = input.nextDouble();
    System.out.println("Enter the number of payments: ");
    double numberofPayments = input.nextDouble();

    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(annualInterestRate/12)^-numberofPayments))));







    }
    }
    I dont know how to enter this formula maybe there is a simpler way?

    --- Update ---

    In which PMT is the monthly payment, P is the listed price of the house, D is the
    down payment, r is the annual interest rate and m is the number of payments.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Monthly mortgage payment calculator

    It's much easier to help you if we can easily read your code. Please surround your code with code tags:

    [code]

    //..... your code goes here

    [/code]

    Also, regarding your formula, have you had a chance to look at some of the methods available in the Math class? Some will come in handy for you such as Math.pow(....). Please have a look as you won't regret it!

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

     
     
     
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble();
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
    		double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(annualInterestRate/12)^-numberofPayments))));
     
     
     
    		System.out.println( (monthlyPayment+ " is the larger of the two numbers.");
     
     
     
    	}	
    	}


    also Im somewhat aware of the math pow function but dont know how to fit it in this formula(not sure where it would go)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Monthly mortgage payment calculator

    Quote Originally Posted by iamgonge View Post
    also Im somewhat aware of the math pow function but dont know how to fit it in this formula(not sure where it would go)
    Anywhere you see the little ^ in your math formula this can mean x to the power of y, while in Java it means something completely different (and not very useful for your purposes). I'd start there. Good luck!

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    ok tried it didnt work now what( I dont know how to write this code!!!!!!)
    heres what i tried

    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(annualInterestRate/12)math.pow-numberofPayments)))); <----dont know how to enter this last part its a negative exponent

    --- Update ---

    ok i think i got it does this look right?
    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, - numberofPayments)))));

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Monthly mortgage payment calculator

    The Math API will tell you how to use the method, but as in calling *any* method, you will need to use parentheses. You will need to separate your parameters by commas if you have multiple parameters.

    As an aside: please do not multi-post the same question in the forum.

    Quote Originally Posted by iamgonge View Post
    ok i think i got it does this look right?
    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, - numberofPayments)))));
    Test it out and see if the code works as expected.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    so it doesnt seem to be computing correctly i keep getting negative number and they seem quite off, Is my formula wrtitten wrong?

    --- Update ---

    ok so this my program as of this moment, it runs but the math is wrong can someone please check my formula
    [code]


    import java.util.Scanner;

    public class Project1 {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the price of the house: ");
    double housePrice = input.nextDouble();
    System.out.println("Enter the down payment: ");
    double downPayment = input.nextDouble();
    System.out.println("Enter the annual interest rate: ");
    double annualInterestRate = input.nextDouble();
    System.out.println("Enter the number of payments: ");
    double numberofPayments = input.nextDouble();

    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, -numberofPayments)))));


    System.out.println( "The price of the house is: " + housePrice);
    System.out.println( "Your down payment is :" + downPayment);
    System.out.println( "Your annual interest rate is :" + annualInterestRate);
    System.out.println( "Your number of payments are :" + numberofPayments);
    System.out.println( "Your monthly payment is :" + monthlyPayment);







    }
    }
    /code

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    it doesnt seem to be computing correctly
    ...
    check my formula
    Try debugging the code by breaking the compound expression up into separate single expressions and printing the results of each. The print out should show you where the code is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, -numberofPayments)))));

    this is my formula and its not working please help?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    Try debugging the code by breaking the compound expression up into separate single expressions and printing the results of each. The print out should show you where the code is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    ok changed it still doesnt work please check

     
     
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble();
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
    		double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(Math.pow(annualInterestRate/12, -numberofPayments))))));
     
     
    		System.out.println( "The price of the house is: " + housePrice);
    		System.out.println( "Your down payment is :" + downPayment);
    		System.out.println( "Your annual interest rate is :" + annualInterestRate);
    		System.out.println( "Your number of payments are :" + numberofPayments);		
    		System.out.println( "Your monthly payment is :" + monthlyPayment);
     
     
     
     
     
     
     
    	}	
    	}

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Monthly mortgage payment calculator

    Please edit your post above and fix your code tags. Your code is difficult to read. If it doesn't work, then please show what you've done to try to debug it (as per Norm's recommendations above), and the results of these efforts.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    ok changed it still doesnt work please check
    Did you do what I suggested to find the problem? I don't see it in the posted code.

    Try debugging the code by breaking the compound expression up into separate single expressions and printing the results of each. The print out should show you where the code is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    im not faqmiliar with this formula i dont know how to break it down in java code so i cant do what your saying are you saying assign this a variable (housePrice - downPayment) then assign this a variable (annualInterestRate/12)) and so on, ? I dont understand

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    saying assign this a variable (housePrice - downPayment) then assign this a variable (annualInterestRate/12)) and so on
    Yes, that is exactly what I'm saying.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    ok this what i got i broke the formula into a top and a bottom
    then printed out the results: heres the code
     
     
    import java.util.Scanner;
     
    	public class Project1 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the price of the house: ");
    		double housePrice = input.nextDouble();
    		System.out.println("Enter the down payment: ");
    		double downPayment = input.nextDouble();
    		System.out.println("Enter the annual interest rate: ");
    		double annualInterestRate = input.nextDouble();
    		System.out.println("Enter the number of payments: ");
    		double numberofPayments = input.nextDouble();
     
     
    		double tempTop = ((housePrice - downPayment) * (annualInterestRate/12));
    		double tempBottom = ((1-(1+(Math.pow(annualInterestRate/12, -numberofPayments)))));
    		double monthlyPayment = (tempTop / tempBottom);
    		System.out.println( "The price of the house is: " + housePrice);
    		System.out.println( "Your down payment is :" + downPayment);
    		System.out.println( "Your annual interest rate is :" + annualInterestRate);
    		System.out.println( "Your number of payments are :" + numberofPayments);		
    		System.out.println( "Your monthly payment is :" + monthlyPayment);
    		System.out.println( "");
    		System.out.println( "Top of equation equals :" + tempTop);
     
    		System.out.println( "Bottom of equation equals :" + tempBottom);	
     
     
    	}	
    	}

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    then printed out the results:
    When you look at the values that were printed out, were they correct?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    no

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    Which ones were wrong?
    What are the the correct values?

    Which simple, single expression computed the wrong value?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Monthly mortgage payment calculator

    Quote Originally Posted by iamgonge View Post
    double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, -numberofPayments)))));
    Even try solving the problem, using a pen and paper. Make sure that its correct. For example :
    housePrice = 30000.00
    downPayment = 5000.00

    then write it out like ( ((30000.00 - 50000.00) * ...

    make sure you get the result you expect. Then compare this result with Java programs result.

  21. #21
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    ok i did the calculations on a calculator and they do equal each other, I bottom number keeps turning back a negative number which i dont think is right.

    the formula im supposed to use is written on my paper like this PMT = (P - D)*r/12
    __________
    (1-(1+r/12)^-m) where P is the listed price of the house, D is the
    down payment, r is the annual interest rate and m is the number of payments.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    Which expression returns the wrong value? and what value does it return?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Monthly mortgage payment calculator

    the bottom returns a negative which then turns the final number to a negative monthly payments cant be negative so something is wrong

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Monthly mortgage payment calculator

    Can you post the code for "the bottom" expression and the value it returned?
    also print out the values of the variables used in "the bottom" expression.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Monthly mortgage payment calculator

    PMT = (P - D)*r/12
    ______________
    (1-(1+r/12)^-m)

    Ok this is the formula. Do me a favour and post up what values you are using and the expected result.
    Also annualInterestRate, is that a percentage % or a monetary value (£, $).

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with my code for calculating monthly payment on a loan
    By Marcella74 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 25th, 2012, 09:26 AM
  2. Arrays for mortgage calc
    By kprofgold in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2012, 01:01 AM
  3. Monthly Mortage Payment Calculator
    By lockdown in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 17th, 2011, 11:54 PM
  4. Mortgage Calculator Issue
    By coyboss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 5th, 2011, 09:12 AM
  5. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM