import javax.swing.*; import java.text.*; class Loan { public static void main(String[] args) { String initLoanStr, interestStr, monthPayStr; double initLoan, interest, monthPay, amountLeft; double minMonthPay, monthInterest,totalInterest; int month; // Get Data initLoanStr = JOptionPane.showInputDialog(null,"Enter Loan Amount"); interestStr = JOptionPane.showInputDialog(null,"Enter Annual Interest"); initLoan = Double.parseDouble(initLoanStr); interest = Double.parseDouble(interestStr); interest = interest/100.0; // convert to decimal minMonthPay = initLoan*interest/12.0; DecimalFormat df = new DecimalFormat("0.00"); monthPayStr = JOptionPane.showInputDialog(null,"Enter Monthly Payment greater than $" + df.format(minMonthPay)); monthPay = Double.parseDouble(monthPayStr); month = 0; amountLeft = initLoan; totalInterest = 0; while (amountLeft > 0.01) { monthInterest = amountLeft*interest/12.0; totalInterest = totalInterest + monthInterest; amountLeft = amountLeft - (monthPay-monthInterest); month = month + 1; } JOptionPane.showMessageDialog(null,"It will take " + month + " months to pay off the loan\n" + "Total interest paid will be $" + df.format(totalInterest)); System.exit(0); } }