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 9 of 9

Thread: Whats wrong with my code?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy 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


    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;
            }
        }
    }


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default 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.
    Simplicity calls for Complexity. Think about it.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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...

  4. #4
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default 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.

    So reply like this:

    Correct Output:
    1
    2
    3

    My Output:
    1.5
    2.5
    3.5
    Simplicity calls for Complexity. Think about it.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my code?

    My new code:


    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

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default 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.
    Simplicity calls for Complexity. Think about it.

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  9. #9
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Whats wrong with my code?

    Quote Originally Posted by Bryan29 View Post
    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.
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. whats wrong with my code.
    By jove in forum Object Oriented Programming
    Replies: 3
    Last Post: July 30th, 2011, 11:45 PM
  2. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM
  3. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:59 PM
  4. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 05:34 PM
  5. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM