0

I have to create an Car Payment app in java that prompts user for principal owing (P), interest rate (r) and the number of monthly payments (m) The monthly car payment MUST be calculated using this formula:

P(r/12)/(1-(1+r/12)^-m)

This is what I have so far...

 import java.util.Scanner;
 import java.lang.Math; //importing Scanner, Math, and NumberFormat classes
 import java.text.NumberFormat;

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

        double principal, rate, numberOfMonths, payment;    //declaring varibles

        System.out.print("Principal: ");
        principal=input.nextDouble();
        System.out.print("Interest Rate: ");    //requesting and storing user input
        rate=input.nextDouble();
        System.out.print("Number of monthly payments: ");
        numberOfMonths=input.nextDouble();
        input.close(); //closing Scanner

        payment=principal*(rate/12)/(1-(1+rate/12*)Math.pow(payment, -numberOfMonths)); //calculating monthly payment. Error in this line

        NumberFormat money =NumberFormat.getCurrencyInstance(); //Formatting output
        System.out.println("The monthly payment is:" (money.format(payment));



    }
 }

It does not compile, and I'm really frustrated because I have spent a long time and I cannot figure this out.

Any help is appreciated

Bob
  • 3
  • 1
  • 2
    No doubt someone will help you with your _specific_ problem but I'd just like to mention that comments are meant to _add_ useful information. To me, something `import Math; // import the math library` doesn't really seem to do it :-) And, f you're getting an error during compile, it _may_ be useful if you post it as well so we don't have to guess or type your code into Eclipse or what not. – paxdiablo Jan 08 '15 at 22:33
  • Here's your equation with the syntax corrected: payment=principal*(rate/12)/(1-Math.pow(1+rate/12,-1*numberOfMonths)); – pmorken Jan 08 '15 at 22:40
  • `(1+rate/12*)` has a trailing `*` that has no right operand. I assume you want that to be `(1+rate/12)*` instead? – Oli Jan 08 '15 at 22:41

2 Answers2

1

I think it would be nice if you break your formula into little pieces

   double rate1 = rate / 12 / 100;    // monthly interest rate
    double numberOfMonths = 12 * Y;         // number of months

    double payment  = (Principal * rate1) / (1 - Math.pow(1+rate1, -numberOfMonths));

I hope that helps

Abdi
  • 1,298
  • 1
  • 9
  • 10
0

Formula

What you have, and the errors:

payment=principal*(rate/12)/(1-(1+rate/12*)Math.pow(payment, -numberOfMonths));
  • binary operator * has no second argument in the expression (1+rate/12*)
  • uninitialized payment variable used as first argument to Math.pow()
  • required formula P(r/12)/(1-(1+r/12)^-m) is not realized by the above statement

What it should be for P(r/12)/(1-(1+r/12)^-m):

payment = principal * rate/12 / (1 - Math.pow(1 + rate/12, -numberOfMonths));

Output

What you have, and the errors:

System.out.println("The monthly payment is:" (money.format(payment));
  • String concatenation operator + is missing between literal string and formatted payment
  • It's not clear whether interest rate is interpreted as a percentage or straight decimal

What it should be for clarity:

System.out.println("Rate: " + NumberFormat.getPercentInstance().format(rate));
System.out.println("Payment: " + NumberFormat.getCurrencyInstance().format(payment));
gknicker
  • 5,509
  • 2
  • 25
  • 41