I am currently working on a while loop that should get the following information from the user- loan amount, annual interest, and monthly payment. It should compare monthly interest to the monthly payment and if the interest for the first month exceeds or is equal to the designated payment, a dialog opens up asking for a larger amount until the monthly payment is higher than the interest.
It seems to work fine in certain cases (if i type 1000 for the loan, 12% interest, and any payment below 121, the program asks for a larger payment). But if I enter a loan amount and a monthly payment that are close to eachother (500 loan amount 400 monthly payment) i receive the error dialog stating that I did not enter a large enough amount. Huh!?
Here’s my code if you can take a look. Thanks!
package program6;
import javax.swing.JOptionPane;
public class LoanAmount2 {
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);
annualInterestRate = 1 + (annualInterestRate / 100);
String monthlyPaymentString = JOptionPane.showInputDialog(null, "Enter the amount for your monthly payment.");
double monthlyPayment = Double.parseDouble(monthlyPaymentString);
double monthlyInterestRate = 1 + (annualInterestRate / 12);
double monthlyInterest = loanAmount * monthlyInterestRate;
while (monthlyInterest >= monthlyPayment) {
monthlyPaymentString = JOptionPane.showInputDialog(null, "Your payment only covers the interest on your loan. Please enter a larger amount for your monthly payment.");
monthlyPayment = Double.parseDouble(monthlyPaymentString);
break;
}
}
}
I just ran this script and noticed your problem. The line:
Should read:
If you add one to this your while loop will always evaluate to true, because you are multiplying the full loan amount by 1.xx