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

Thread: i'm making a logical error here but i'm not seeing it.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i'm making a logical error here but i'm not seeing it.

    The goal of this program is to calculate how much the total payment on a loan will be, given a user-entered interest rate, user-entered amount to be borrowed, and user-entered monthly payment. It should also be noted that the annual interest rate changes as the loan decreases with payment (I accounted for this in the second while loop by writing annualInterestRate = annualInterestRate * loanAmount. Is this correct?) If the interest rate given determines that the monthly interest is not higher than the monthly payment, a new loop begins that adds up monthly payments until the total left on the loan is 0.

    There aren't any syntax errors in the coding to my knowledge, it mostly runs fine. It's difficult to tell if my coding in the second while loop is correct because it would take a long time to work out that math by hand, but I did notice one glaring problem.

    If I enter 500 as the loan amount, and 400 as a payment, it gives me the error dialog that my monthly payment is too low. I don't really know why this would be, but I'm guessing that something in the code is getting thrown off when the monthly payment is too close to the loan.

    Advice would be appreciated! Thanks.

    package javaapplication18;
    import javax.swing.JOptionPane;
     
    public class LoanPayment {
     
        public static void main(String[] args) {
    String loanAmountString = JOptionPane.showInputDialog(null, "Enter the amount for your loan.");
    double loanAmount = Double.parseDouble(loanAmountString);
    String annualInterestString = JOptionPane.showInputDialog(null, "Enter your annual interest rate.");
    double annualInterestRate = Double.parseDouble(annualInterestString);
    String monthlyPaymentString = JOptionPane.showInputDialog(null, "Enter the amount for your monthly payment.");
    double monthlyPayment = Double.parseDouble(monthlyPaymentString);
    double monthlyInterestRate = annualInterestRate / 12;
    double monthlyInterest = loanAmount * monthlyInterestRate;
    while (monthlyInterest >= monthlyPayment) {
    String newMonthlyPaymentString = JOptionPane.showInputDialog(null, "Your payment only covers the interest on your loan. Please enter a larger amount for your monthly payment.");
    monthlyPayment = Double.parseDouble(newMonthlyPaymentString);
           break;
    } 
    double totalPaid = monthlyPayment;
    int month = 1;
    while (totalPaid <= loanAmount) {
        totalPaid = totalPaid + monthlyPayment;
        annualInterestRate = annualInterestRate * loanAmount;
        loanAmount = loanAmount + monthlyInterest;
        loanAmount = loanAmount - totalPaid;
        month++;
      if (monthlyPayment > loanAmount)
          totalPaid = totalPaid + loanAmount;
        month++;
    }
    JOptionPane.showMessageDialog(null, "Congratulations! You have paid off your " + month + " month loan, totalling " + totalPaid + ". Thank you.");
        }
    }
    Last edited by qrts; December 5th, 2011 at 01:33 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: i'm making a logical error here but i'm not seeing it.

    while (monthlyInterest >= monthlyPayment) {
    String newMonthlyPaymentString = JOptionPane.showInputDialog(null, "Your payment only covers the interest on your loan. Please enter a larger amount for your monthly payment.");
    monthlyPayment = Double.parseDouble(newMonthlyPaymentString);
           break;
    }
    Simply see the loop's condition. We don't know your business rules so we can't suggest you anything. Program is simply working fine as programmed to do. When you multiply your loanAmount with the interest, it will for sure increase by the amount monthlyPayment (in the case where you enter loanAmount greater than monthlyAmount)

Similar Threads

  1. Logical error and having trouble HELP!
    By DeathStrike in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2011, 07:02 PM
  2. I have a logical error
    By n00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 02:52 PM
  3. Sum of series program logical error
    By Neel0075 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 11:26 AM
  4. [SOLVED] Help making an error message.
    By Lost_Secret in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 1st, 2011, 03:48 PM
  5. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 06:58 AM