Java Car Loan Calculator

So I completed the exercise, but I got the wrong answer. It gave me “The car can be paid in full” instead of “233.”

I even looked at the walkthrough but it’s still messing up.

public class CarLoan {

  public static void main(String[] args) {

  int carLoan = 1000;

  int loanLength = 3;

  int interestRate = 5;

  int downPayment = 2000;

    if(loanLength <= 0 || interestRate <= 0) {

      System.out.println("Error! You must take out a valid car loan.");

    } else if(downPayment >= carLoan) {

      System.out.println("The car can be paid in full.");


    else {

    int remainingBalance = carLoan - downPayment;

    int months = loanLength * 12;

    int monthlyBalance = remainingBalance / months;

    int interest = monthlyBalance * interestRate / 100;

    int monthlyPayment = monthlyBalance + interest;




1 Like

Hello @arc3009184213 , welcome to the forums! Unfortunately, I don’t have PRO, so I can’t see the project. Could you tell me how the car loan is being calculated?

You’re statement “if (downPayment >= carLoan)” Will always evaluate as true because the downPayment is greater than or equal to the carLoan.

In Other words;

2000 >= 1000;

so it prints out the statement and doesn’t advance. Try changing the loan amount to a value greater than 2000.

1 Like

Hey @arc3009184213 , it’s been a little while since you originally posted this question. Could you come back to let us know if the above reply helped you?

I found a more accurate formula for calculating the monthly payment on a car loan … using compound interest instead of the simple interest that is used in the instructions.
The formula is called “finding the payment of an ordinary simple annuity”.
I had to have import java.lang.Math; at the beginning in order to do exponents.
Calculating the monthly payment changes from the stuff given in the instructions to be the following:

double i = (double)interestRate / 12 / 100;  // monthly rate as a decimal, not a percent
double monthlyPayment = remainingBalance * i / (1.0 -  Math.pow(1.0 + i, -months);