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

Can someone with more java skills than I have please tell me why I keep getting

ID: 3910248 • Letter: C

Question

Can someone with more java skills than I have please tell me why I keep getting an error when I try to run this code?

import java.util.*;

public class InvestmentCalculator{

{

public static void display(double p, double r, int q)

{

System.out.printf("%-20s %-20s %-20s %-20s%n", "Quarter","Beginning", "Interest", "Ending");

System.out.printf("%-20s %-20s %-20s %-20s%n", "Number", "Balance","Earned", "Balance");

double amount=p;

for(int i=1;i<=q;i++)

{

double interest=(amount*r)/100*0.25;

double roundOff = (double) Math.round(interest * 100) / 100;

System.out.printf("%-20d $%-19.2f $%-19.2f $%-18.2f%n", i, amount,roundOff, amount+roundOff);

amount+=roundOff;

}

}

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

int q;

double p,r;

while(true)

{

while(true)

{

System.out.println("Enter number of quarters from 1 to 10");

q=in.nextInt();

if(q>=1 && q<=10)

break;

else System.out.println("Number of quarters must be between 1 and 10 inclusive.");

}

while(true)

{

System.out.println("Enter the beginning principal balance greater than 0");

p=in.nextDouble();

if(p>0)

break;

else System.out.println("Beginning balance must be greater than zero. ");

}

while(true)

{

System.out.println("Enter the interest rate percentage without the percent sign greater than 0% and less than/equal to 20%.");

r=in.nextDouble();

if(r>=1 && r<=20)

break;

else System.out.println("Interest rate must be between 1 and 20 inclusive.");

}

System.out.println("You entered a principal balance of $"+p+" for"+q+" quarters at "+r+"% interest.");

System.out.println("Is this correct?(y/n)");

String d=in.nextLine();

String c=in.nextLine();

if(c.equals("y"))

break;

}

display(p,r,q);

}

}

Here is the error I receive...

InvestmentCalculator.java:8: error: illegal start of expression

public static void display(double p, double r, int q)

^

1 error

Explanation / Answer

Answer:

you have unwanted curly brace above the method that i have removed. it is working fine now.

InvestmentCalculator.java

import java.util.*;

public class InvestmentCalculator{

public static void display(double p, double r, int q)

{

System.out.printf("%-20s %-20s %-20s %-20s%n", "Quarter","Beginning", "Interest", "Ending");

System.out.printf("%-20s %-20s %-20s %-20s%n", "Number", "Balance","Earned", "Balance");

double amount=p;

for(int i=1;i<=q;i++)

{

double interest=(amount*r)/100*0.25;

double roundOff = (double) Math.round(interest * 100) / 100;

System.out.printf("%-20d $%-19.2f $%-19.2f $%-18.2f%n", i, amount,roundOff, amount+roundOff);

amount+=roundOff;

}

}

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

int q;

double p,r;

while(true)

{

while(true)

{

System.out.println("Enter number of quarters from 1 to 10");

q=in.nextInt();

if(q>=1 && q<=10)

break;

else System.out.println("Number of quarters must be between 1 and 10 inclusive.");

}

while(true)

{

System.out.println("Enter the beginning principal balance greater than 0");

p=in.nextDouble();

if(p>0)

break;

else System.out.println("Beginning balance must be greater than zero. ");

}

while(true)

{

System.out.println("Enter the interest rate percentage without the percent sign greater than 0% and less than/equal to 20%.");

r=in.nextDouble();

if(r>=1 && r<=20)

break;

else System.out.println("Interest rate must be between 1 and 20 inclusive.");

}

System.out.println("You entered a principal balance of $"+p+" for"+q+" quarters at "+r+"% interest.");

System.out.println("Is this correct?(y/n)");

String d=in.nextLine();

String c=in.nextLine();

if(c.equals("y"))

break;

}

display(p,r,q);

}

}

Output:

Enter number of quarters from 1 to 10
5
Enter the beginning principal balance greater than 0
1000
Enter the interest rate percentage without the percent sign greater than 0% and less than/equal to 20%.
8
You entered a principal balance of $1000.0 for5 quarters at 8.0% interest.
Is this correct?(y/n)
y
Quarter Beginning Interest Ending   
Number Balance Earned Balance
1 $1000.00 $20.00 $1020.00
2 $1020.00 $20.40 $1040.40
3 $1040.40 $20.81 $1061.21
4 $1061.21 $21.22 $1082.43
5 $1082.43 $21.65 $1104.08

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