A Simple Car Loan Payment Calculator - Step 15


#1

I'm getting 222 as the monthly payment instead of 233. I've double checked my code. Anyone else not getting 233?

public class CarLoan {
      public static void main(String[] args) {
    
      /* NOTE: I get 223 instead of 233 */
    
      /* This program calculates a car loan for whoever wants one of those
       */
    
      int carLoan = 10000;
      int loanLength = 3;
      int interestRate = 5;
      int downPayment = 2000;
    
      if (loanLength <= 0 || interestRate <= 0) {
        System.out.println("Error! I see you don't want a 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;
        System.out.println(monthlyPayment);
      }
    
      }
    }

#2

Could you post the code and inputs?

Might be able to see why.


#5

I edited the code and it seems to be the down payment that needs to be set at 1600 instead of 2000 this should fix the issue:

New code:

public class CarLoan {
public static void main(String[] args) {

int carLoan = 10000;
int loanLength = 3;
int interestRate = 5;
int downPayment = 1600;

if (loanLength <= 0 || interestRate <= 0) {
System.out.println("Error! I see you don't want a car loan. :wink:");
} 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;
System.out.println(monthlyPayment);
}

}
}


#6

The instructions state to use 2000 for downPayment. That's why I'm confused.

I figure if something is wrong in my code it's something with the order of operations in my calculations. I'll recheck those..


#7
int interest = monthlyBalance * (interestRate / 100);

This line was messing me up because of the way Java handles int division. interestRate / 100 = 0, therefore interest = 0. Right way to deal with this:

(monthlyBalance * interestRate) / 100;


#8

Ah, nice.

I was sitting here solving on paper and trying to reproduce the problem on my side but I'm glad you solved it.


#9

Thanks. I put monthlyBalance * interestRate / 100 and it didn't show any result. Putting ( ) was the solution!
But I'm curious about Why.. Maybe because ' * ' and ' / ' has the same priority so it needed ( ) to know which one to calculate first.


#12