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."); } }