Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to build a mortgage payment calculator, and one of my instructions i

ID: 3666407 • Letter: I

Question

I am trying to build a mortgage payment calculator, and one of my instructions is: The application should accept decimal entries for the loan amount and interest rate entries.

I have the following code TRYING to accepts decimal entries for the loan amount but when I enter a value like 500000.65 the program crashes.

import java.text.NumberFormat;
import java.util.Scanner;

public class MortgagePaymentCalculator {

public static double calculateMonthlyPayment(
double loanAmount, int years, double interestRate) {

// Convert interest rate into a decimal
// eg. 6.5% = 0.065

interestRate /= 100.0;

// Monthly interest rate
// is the yearly rate divided by 12

double monthlyInterestRate = interestRate / 12.0;

// The length of the term in months
// is the number of years times 12

int months = years * 12;

// Calculate the monthly payment
// Typically this formula is provided so
// we won't go into the details

// The Math.pow() method is used calculate values raised to a power

double monthlyPayment =
loanAmount*monthlyInterestRate /
(1-1/Math.pow(1 + monthlyInterestRate, months));

return monthlyPayment;
}
  
public static void main(String[] args) {

// Scanner is a great class for getting
// console input from the user

Scanner scanner = new Scanner(System.in);

// Prompt user for details of loan

System.out.print("Enter loan amount: ");
double loanAmount = scanner.nextInt();

System.out.print("Enter loan term (in years): ");
int years = scanner.nextInt();

System.out.print("Enter interest rate: ");
double interestRate = scanner.nextDouble();

// Display details of loan

double monthlyPayment =
calculateMonthlyPayment((double) loanAmount, (int)years, (double)interestRate);


// NumberFormat is useful for formatting numbers
// In our case we'll use it for
// formatting currency and percentage values

NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
NumberFormat interestFormat =
NumberFormat.getPercentInstance();

// Display details of the loan

System.out.println("Loan Amount: "+
currencyFormat.format(loanAmount));
System.out.println("Yearly Interest Rate: "+
interestFormat.format(interestRate));
System.out.println("Number of Years: "+
years+" years");
System.out.println("Monthly Payment: "+
currencyFormat.format(monthlyPayment));

}

}

Explanation / Answer

use this line

double loanAmount = scanner.nextDouble();

in place of

double loanAmount = scanner.nextInt();

it will work

MortgagePaymentCalculator.java

import java.text.NumberFormat;
import java.util.Scanner;

public class MortgagePaymentCalculator {

   public static double calculateMonthlyPayment(
      double loanAmount, int years, double interestRate) {

      // Convert interest rate into a decimal
      // eg. 6.5% = 0.065

      interestRate /= 100.0;

      // Monthly interest rate
      // is the yearly rate divided by 12

      double monthlyInterestRate = interestRate / 12.0;

      // The length of the term in months
      // is the number of years times 12

      int months = years * 12;

      // Calculate the monthly payment
      // Typically this formula is provided so
      // we won't go into the details

      // The Math.pow() method is used calculate values raised to a power

      double monthlyPayment =
         loanAmount*monthlyInterestRate /
            (1-1/Math.pow(1 + monthlyInterestRate, months));

      return monthlyPayment;
   }

   public static void main(String[] args) {

      // Scanner is a great class for getting
      // console input from the user

      Scanner scanner = new Scanner(System.in);

      // Prompt user for details of loan

      System.out.print("Enter loan amount: ");
      double loanAmount = scanner.nextDouble();

      System.out.print("Enter loan term (in years): ");
      int years = scanner.nextInt();

      System.out.print("Enter interest rate: ");
      double interestRate = scanner.nextDouble();

      // Display details of loan

      double monthlyPayment =
         calculateMonthlyPayment((double) loanAmount, (int)years, (double)interestRate);


      // NumberFormat is useful for formatting numbers
      // In our case we'll use it for
      // formatting currency and percentage values

      NumberFormat currencyFormat =
         NumberFormat.getCurrencyInstance();
      NumberFormat interestFormat =
         NumberFormat.getPercentInstance();

      // Display details of the loan

      System.out.println("Loan Amount: "+
         currencyFormat.format(loanAmount));
      System.out.println("Yearly Interest Rate: "+
         interestFormat.format(interestRate));
      System.out.println("Number of Years: "+
         years+" years");
      System.out.println("Monthly Payment: "+
         currencyFormat.format(monthlyPayment));

   }

}

Output

Enter loan amount: 500000.65
Enter loan term (in years): 10
Enter interest rate: 10
Loan Amount: Rs.500,000.65
Yearly Interest Rate: 1,000%
Number of Years: 10 years
Monthly Payment: Rs.6,607.55

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote