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

I am trying to run my final code, however I am recieving an error: \"Illegal sta

ID: 3667170 • Letter: I

Question

I am trying to run my final code, however I am recieving an error: "Illegal start of expression" on the line of code that is in bold below. Not sure what I am missing or need to add.

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

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

// 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 sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// Prompt user for details of loan
  
double loanAmount = getDoubleWithinRange(sc,
"Enter loan amount: ", 0, 1000000);

int years = getIntWithinRange(sc, "Enter loan term (in years): ", 0, 100);

double interestRate = getDoubleWithinRange(sc, "Enter interest rate: ",
0, 20);

// 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();
  


// Display details of the loan

System.out.println("Loan Amount: "+
currencyFormat.format(loanAmount));
System.out.println("Yearly Interest Rate: "+
interestRate + '%');
System.out.println("Number of Years: "+
years+" years");
System.out.println("Monthly Payment: "+
currencyFormat.format(monthlyPayment));
  
System.out.print("Enter another test score? (y/n): ");
choice = sc.next();

// Check if the anwer choice is neither equal to y nor n.
while(!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y"))
{
System.out.print("Invalid input, Continue?: ");
choice = sc.next();
  
}
  
public static double getDouble(Scanner sc, String prompt)

{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}

public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}


  
  
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
  
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}   
}
  
  
  

Explanation / Answer

There are two closing brackets missing. Before the bolded line write two closing brackets. It has no other errors. It's compiling perfectly now.

Edited code if required:

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

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

// 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 sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// Prompt user for details of loan
  
double loanAmount = getDoubleWithinRange(sc,
"Enter loan amount: ", 0, 1000000);

int years = getIntWithinRange(sc, "Enter loan term (in years): ", 0, 100);

double interestRate = getDoubleWithinRange(sc, "Enter interest rate: ",
0, 20);

// 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();
  


// Display details of the loan

System.out.println("Loan Amount: "+
currencyFormat.format(loanAmount));
System.out.println("Yearly Interest Rate: "+
interestRate + '%');
System.out.println("Number of Years: "+
years+" years");
System.out.println("Monthly Payment: "+
currencyFormat.format(monthlyPayment));
  
System.out.print("Enter another test score? (y/n): ");
choice = sc.next();

// Check if the anwer choice is neither equal to y nor n.
while(!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y"))
{
System.out.print("Invalid input, Continue?: ");
choice = sc.next();
  
}
}

}
   public static double getDouble(Scanner sc, String prompt)

{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}

public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}


  
  
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
  
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}   
}

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