public class Gradesv2
{
    public static void main(String[ ] args)
    {
        //local variables
        int numTests = 0;       //counts number of tests
        int testgGrades = 0;    //individual test grade
        int totalPoints = 0;    //total points for all test
        double average = 0.0;   //average grade
        int test1 = 95;
        int test2 = 73;
        int test3 = 91;
        int test4 = 82;

        System.out.println("");
        System.out.println("Test # 1 Test Grade: " + test1 + "    Total Points: " + test1 + " = " + totalPoints + "Average Score: " + test1 =  +);
    }//end of main method
}//end of class

Dani AI

Generated

Short answer: the compiler error comes from the malformed print statement — that trailing = +); after the string is not valid Java and causes "illegal start of expression." As pointed out, the problem is the broken token sequence; the Java parser sees something it can't interpret as part of a valid statement. Also note 's comment about Public — Java keywords are lowercase public, and the file must be compiled with the Java compiler (not a C++ compiler) as asked and confirmed the language.

Fix the logic by computing totals/average first, then build the output string (use + only to concatenate values or put literal = inside quotes). Example fixes:

int totalPoints = test1 + test2 + test3 + test4;
int numTests = 4;
double average = totalPoints / (double) numTests;

System.out.println();
System.out.println("Test #1 Test Grade: " + test1 + "    Total Points: " + totalPoints + "    Average Score: " + average);

Or use formatted output:

System.out.printf("Test #%d Grade: %d   Total: %d   Average: %.2f%n", 1, test1, totalPoints, average);

Quick troubleshooting checklist:

  • Remove the stray = +); and any other stray operators. If you want an equals sign in output, put " = " inside the string.
  • Check earlier lines for unclosed quotes, parentheses, or braces — the compiler often flags the next token after the real problem.
  • Compile with javac Gradesv2.java (not a C++ compiler) and let the javac error line guide you.
  • Clean up unused/typo variables (e.g., testgGrades) and cast to double when computing averages to avoid integer division.

Recommended Answers

All 6 Replies

Let's start at line 0. Because I don't know all languages I'm not going to make an assumption here so what programming language is this? And what line is giving the error?

It would also help if you posted in the appropriate forum.

Looks like Java

Why did you put the keyword Public there?

Whats wrong with declaring a class public?

I keep getting this illegal start of expression and can't figure out why.

It's because line 16 ends with = +); which makes no sense at all (and isn't valid Java syntax)

@JC. I put this code into a C++ compiler and it couldn't budge past that line until I removed it. Might be the old machine I was on but I thought we'd start with the first line.

This is java code.

commented: When I first read this, I didn't see that tag. Anyhow, will re-read. +15

@JC. I put this code into a C++ compiler..

Apart from the odd chars right at the end of line 16 this is 100% valid Java,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.