# Whats wrong with my code?

• October 4th, 2011, 11:24 PM
Bryan29
Whats wrong with my code?
Hello, I'm trying to make a program that compares loans with interest rates from 5% - 8%, with an increment of 1/8.
The output should look something like:

Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5% 188.71 11322.74
5.125% 189.28 11357.13
5.25% 189.85 11391.59
...
7.85% 202.16 12129.97
8.0% 202.76 12165.83

I believe that I have gotten the correct Interest Rate, I just can't get the correct numbers for Monthly and Total Payments (I've copied the formula exactly as it shows it in the book within another program). I also can't figure out how to make them adjust with the interest rate.

I'm new to loops so any kind of help will be appreciated,

Thanks :)

Code Java:

```import java.util.Scanner;   public class Exercise04_21 {   public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Loan Amount: "); int loan = input.nextInt();   System.out.print("Number of Years: "); int years = input.nextInt();   System.out.printf("%10s %10s %10s\n", "Interest Rate", "Monthly Payment", "Total Payment");   double interest = 5.0;   double monthlyPay = loan * (interest / 100) / (1 - 1 / Math.pow(1 + (interest / 100), years * 12));   double total = monthlyPay * 12 * years;   while (interest <= 8.0) {     System.out.printf("%10.2f %10.2f %10.2f%n", interest, monthlyPay, total);     interest += 0.125; } } }```
• October 4th, 2011, 11:57 PM
Staticity
Re: Whats wrong with my code?
Your error is arising with your "monthlyPay" equation. What is the given formula for Monthly Pay? Make sure it matches with what you wrote.
• October 5th, 2011, 12:11 AM
Bryan29
Re: Whats wrong with my code?
The formula is:

(loanAmount * monthlyInterestRate) / 1 - (1 / (1 + monthlyInterestRate)^numberOfYears*12)

, copied from the book. I entered it in using several different variables and 1 long variable and it still won't work... :(
• October 5th, 2011, 12:45 AM
Staticity
Re: Whats wrong with my code?
Let's start by comparing your output values to the actual answer. So input the same numbers as the example into your equation, and list the values.

Correct Output:
1
2
3

My Output:
1.5
2.5
3.5
• October 5th, 2011, 01:14 AM
Bryan29
Re: Whats wrong with my code?
My new code:

Code Java:

```import java.util.Scanner;   public class Exercise04_21 {   public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Loan Amount: "); int loan = input.nextInt();   System.out.print("Number of Years: "); int years = input.nextInt();           double monthlyPayment; double totalPayment;   System.out.println("Interest Rate\tMonthly Payment\tTotal Payment"); double interest = 5.0; while (interest <= 8.0) {   monthlyPayment = (int) (loan * interest / (1 - (Math.pow(1 / (1 + interest), years * 12))) * 100) / 100; totalPayment = (int) (((monthlyPayment * 12) * years) * 100) / 100;   System.out.println(interest + "\t\t" + monthlyPayment + "\t\t" + totalPayment);   interest +=0.125; } } }```

The output when I enter 10000 for the loan and 5 for the year (same as the example in the first post) is:

Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5.0 50000.0 3000000.0
5.125 51250.0 3075000.0
5.25 52500.0 3150000.0
5.375 53750.0 3225000.0
5.5 55000.0 3300000.0
5.625 56250.0 3375000.0
5.75 57500.0 3450000.0
5.875 58750.0 3525000.0
6.0 60000.0 3600000.0
6.125 61250.0 3675000.0
6.25 62500.0 3750000.0
6.375 63750.0 3825000.0
6.5 65000.0 3900000.0
6.625 66250.0 3975000.0
6.75 67500.0 4050000.0
6.875 68750.0 4125000.0
7.0 70000.0 4200000.0
7.125 71250.0 4275000.0
7.25 72500.0 4350000.0
7.375 73750.0 4425000.0
7.5 75000.0 4500000.0
7.625 76250.0 4575000.0
7.75 77500.0 4650000.0
7.875 78750.0 4725000.0
8.0 80000.0 4800000.0
• October 5th, 2011, 01:18 AM
Bryan29
Re: Whats wrong with my code?
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5.0/// / 50000.0--- 3000000.0
5.125// 51250.0--- 3075000.0
5.25/// 52500.0--- 3150000.0
5.375// 53750.0--- 3225000.0
5.5/// 55000.0--- 3300000.0
5.625// 56250.0--- 3375000.0
5.75/// 57500.0--- 3450000.0
5.875// 58750.0--- 3525000.0
6.0//// 60000.0--- 3600000.0
6.125// 61250.0--- 3675000.0
6.25/// 62500.0--- 3750000.0
6.375// 63750.0--- 3825000.0
6.5//// 65000.0--- 3900000.0
6.625// 66250.0--- 3975000.0
6.75/// 67500.0--- 4050000.0
6.875// 68750.0--- 4125000.0
7.0//// 70000.0--- 4200000.0
7.125// 71250.0--- 4275000.0
7.25/// 72500.0--- 4350000.0
7.375// 73750.0--- 4425000.0
7.5/// 75000.0--- 4500000.0
7.625// 76250.0--- 4575000.0
7.75/// 77500.0--- 4650000.0
7.875// 78750.0--- 4725000.0
8.0//// 80000.0--- 4800000.0

Re-posted this to hopefully make it more readable.
• October 5th, 2011, 01:46 AM
Staticity
Re: Whats wrong with my code?
Your monthlyPay equation returns "interest*10000". Follow the equation in your book, I am sure you had some sort of Syntax Error involved with your first equation. This second equation is very hard to follow, and doesn't look very promising. Play around with it some more.
• October 5th, 2011, 01:55 AM
Bryan29
Re: Whats wrong with my code?
I copied that code from an even number example done by my professor, posted on his site. It has to be right..

Could the interest variable that is involved in the loop be affecting it?
• October 5th, 2011, 10:12 AM
Staticity
Re: Whats wrong with my code?
Quote:

Originally Posted by Bryan29
Could the interest variable that is involved in the loop be affecting it?

You need the interest variable within the While loop in order to prevent the loop from looping infinitely. Your monthlyPay equation is not correct if it's not producing the correct Monthly Pay value -> Meaning: Whatever (variable or not) is inside of your equation is incorrect. Debug it by placing "System.out.println()"'s within your loop to check the values as they're coming in.