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.

Results 1 to 7 of 7

Thread: Monthly payment Calculation Error's Please Help

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Smile Monthly payment Calculation Error's Please Help

    Hello I have a project for school and my teacher never did a great job at explaining how to do certain aspects of my code. I've done a lot of research on Java and how to get this to work but none worked and i've been working on this code for over 48 hours trying to get this math to work.
    Basically I have to make a program that can do monthly payment's.
    If I enter a loan amount of :100,000
    with the rate of :6
    and the years being :30
    I should get back about :$599.55

    Also if I enter1: 10,000
    with the rate being 4.5
    years being 3
    I should get back about $ 297.47

    Here's my code.
    package monthypayment2;
    import java.util.Scanner;
     
     
    public class Monthypayment2 {
            private static double monthlypay;
        private static double monthlypay1;
        private static String choice;
     
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
     
     
            double months = 0;
            double numyears;
            double loanamount;
            double rate;
     
     
     
     
            //prompt loan amount
            System.out.print("Enter Loan Amount:");                           //promp the user
            loanamount = in.nextDouble();
     
            //prompt rate
            System.out.print("Enter Rate:");
             rate = in.nextDouble();
     
             //prompt years
            System.out.print("Enter Number of Years:");
            numyears = in.nextDouble();
     
     
          //calculate
           monthlypay1 = loanamount * rate / (1 - 1 / Math.pow(1 + rate, months));
     
            System.out.println("The Monthly Payment is: $" + monthlypay1);
     
            System.out.print("Would you like to calculate again?:(y/n)");
     
            choice = in.next();
            System.out.println();
        }
    }

    Any Help would be appreciated.
    also when I run my code I get the monthyly payment is :$Infinity.
    When i put months as a value of 12 i will get something like $600000488.65


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Monthly payment Calculation Error's Please Help

    The user enters numyears, but you never calculate months, which stays 0.

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Monthly payment Calculation Error's Please Help

    But I thought I needed the months for the Math.pow(1 + rate, Insert another double here);

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,103
    My Mood
    Mellow
    Thanks
    186
    Thanked 656 Times in 643 Posts

    Default Re: Monthly payment Calculation Error's Please Help

    Your response makes no sense. Of course you need months, and you need it to be something other than zero. Look at your code and determine if months is ever set to something other than zero. If not, you should fix that.

  5. #5
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Monthly payment Calculation Error's Please Help

    Ok I think the problem is I don't have the rate as a percentage. I've tried setting my month's to a different number but alas I get the same results.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,103
    My Mood
    Mellow
    Thanks
    186
    Thanked 656 Times in 643 Posts

    Default Re: Monthly payment Calculation Error's Please Help

    I don't know why we're not communicating. The problem is not that mysterious or difficult. The problem is:

    double months = 0;

    and that never changes.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    Ancharius (October 16th, 2013)

  8. #7
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Monthly payment Calculation Error's Please Help

    Well that and I just realized I was diving by 0.. "(1-1 / math.pow(1 + rate, months));" So I fixed the coding in the math to
            monthlypay1 = loanamount * rate / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
    . Thanks for pointing that out.

    --- Update ---

    This is my new code.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package monthypayment2;
    import java.util.Scanner;
     
    /**
     *
     * @author Aj Miles
     */
    public class Monthypayment2 {
        private static double monthlypay1;
        private static String choice;
     
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
     
     
            double months = 0;
            double numyears;
            double loanamount;
            double rate;
     
     
     
     
            //prompt loan amount
            System.out.print("Enter Loan Amount:");                           
            loanamount = in.nextDouble();
     
            //prompt rate
            System.out.print("Enter Rate:");
             rate = in.nextDouble();
     
             //prompt years
            System.out.print("Enter Number of Years:");
            numyears = in.nextDouble();
     
     
          //calculate
           monthlypay1 = loanamount * rate / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
     
     
            System.out.println("The Monthly Payment is: $" + monthlypay1);
     
     
     
     
             If (monthlypay1 < 0) {
                     System.out.println("You need to enter positive numerical data!");
        }
     
             Else {
            System.out.println("Would you like to calculate again?: (Y/N)"); }
     
     
     
        }
     
        }
     
     
        }
    Was seeing if anyone can help me out with the boolean statement.
    I've looked at a couple of examples of boolean statements but wasn't very helpful. (how to use boolean data type in Java)
    Basically what i'm trying to do is if the user enters' say a negative number after everything is done it will tell the user
    to enter positive numerical data. If he enters a positive number it will ask him if he wants to continue calculations again?

Similar Threads

  1. Monthly mortgage payment calculator
    By iamgonge in forum What's Wrong With My Code?
    Replies: 38
    Last Post: March 24th, 2013, 11:59 AM
  2. 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, 08:26 AM
  3. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:16 PM
  4. Monthly Mortage Payment Calculator
    By lockdown in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 17th, 2011, 11:54 PM