hey guys, i have to do this program and i'm having trouble with prompting 2 things outside the loop.

Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8.


Example: If you enter the loan amount 10,000 for 5 years; display a table as follows:


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


This is what I have so far

import javax.swing.JOptionPane;
public class Loans
{
public static void main(String[] args);
{
double rate;
double monthlyPayment;
double loanAmount;
double total;
int numberOfYears;

//rate
// Need to prompt for the loan amount, outside the loop
// Need to prompt for the number of years outside the loop

for(rate = 5.0; rate <=8.0; rate=rate + 0.125)
{

// Enter Loan Amount
String loanAmountString = JOptionPane.showInputDialog("Enter loan amount:");



// convert rate to monthlyrate
// monthlyrate = rate / 1200; // 12 for the number of months and 100 to make a percentage
// calculate monthly payment
// monthlypayment = loanAmount * monthlyrate / ( 1 - 1 (Math.pow(1 + monthlyrate, numberOfYears * 12));
// display monthly payment

// calculate yearly payment
// yearly = monthlypayment * 12;

**if anyone could help i would really appreciate it.

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

String loanAmountString = JOptionPane.showInputDialog("Enter loan amount:");
String loanPeriodString = JOptionPane.showInputDialog("Enter loan period:");

double loanAmount = Double.parseDouble(loanAmountString);
int loanPeriod = Double.parseDouble(loanPeriodString);

System.out.println("Loan Amount: "+loanAmount);
System.out.println("Number of Years: "+loanPeriod);

for (rate = 5.0; rate <=8.0; rate=rate + 0.125) {

}

If you are familiar with exceptions then this would be better, but you can leave it if it confuses you:

double loanAmount = 0.0;
int loanPeriod = 0;

try {
  loanAmount = Double.parseDouble(loanAmountString);
  loanPeriod = Double.parseDouble(loanPeriodString);
} catch (NumberFormatException nfe) {
  System.out.println("Invalid number: "+nfe.getMessage());
  System.exit(1);
} catch (Exception e) {
  System.out.println("Error: "+e.getMessage());
  e.printStackTrace();
  System.exit(2);
}

Inside the loop do the calculations

Edited 6 Years Ago by javaAddict: n/a

What is stopping from writing java code that calculates this:

// convert rate to monthlyrate
// monthlyrate = rate / 1200; // 12 for the number of months and 100 to make a percentage
// calculate monthly payment
// monthlypayment = loanAmount * monthlyrate / ( 1 - 1 (Math.pow(1 + monthlyrate, numberOfYears * 12));
// display monthly payment

// calculate yearly payment
// yearly = monthlypayment * 12;

You think the loop is there just for no reason?

This article has been dead for over six months. Start a new discussion instead.