Home › Java

error: ';' expected (Java) — how to fix

Main.java:5: error: ';' expected
        int total = price * qty
                               ^
1 error

Quick fix: Go to the line and column javac reports (the ^ marker) and also check the line above it: add the missing ; after a statement, close any unbalanced ( or ", and make sure methods are declared at class level, not inside main.

What it means

Every Java statement (declarations, assignments, method calls, return, break) ends with ;. When the parser finishes reading something that could be a complete statement and the next token can't continue it, it reports ';' expected. The ^ shows where it expected the semicolon, which is just after the last valid token. Fix the first error first: one missing token often produces several follow-up errors.

Cause 1: a plain missing semicolon

int total = price * qty     // error points here
System.out.println(total);

Add it:

int total = price * qty;

When the statement continues onto the next line, javac sometimes points at the next line, because only then does it see a token that can't belong to the expression. Always check the line above the reported one.

Cause 2: unbalanced parentheses or quotes

System.out.println("Total: " + (a + b);

Here the inner ( is never closed, so the parser is confused and may report ')' expected or ';' expected. Count the parentheses:

System.out.println("Total: " + (a + b));

An unclosed string literal gives unclosed string literal first; fix that and the ; error usually disappears.

Cause 3: a method declared inside another method

public static void main(String[] args) {
    greet();

    public static void greet() {      // error: illegal start of expression / ';' expected
        System.out.println("hi");
    }
}

Java has no nested methods. Close main before declaring the next method:

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

public static void greet() {
    System.out.println("hi");
}

Cause 4: spaces or hyphens in identifiers

int my value = 3;          // ';' expected: parsed as "int my" followed by a stray word
int my-value = 3;          // parsed as "my - value": also a syntax error

Identifiers can't contain spaces or hyphens. Use camelCase:

int myValue = 3;

A misspelled keyword gives a different error: retrun x; is read as declaring a variable x of type retrun, so javac reports cannot find symbol instead.

Cause 5: syntax from another language

Cause 6: missing ; after a field, enum constants, or an anonymous class

Runnable r = new Runnable() {
    public void run() { }
}           // needs ; because this is still an assignment statement

Write };. Similarly, an enum with fields or methods needs ; after the last constant:

enum Size { S, M, L;  int code() { return ordinal(); } }

Tips

Compile often so errors stay close to what you just typed, and let your IDE format the file (IntelliJ: Ctrl+Alt+L, Eclipse: Ctrl+Shift+F): bad indentation after reformatting shows exactly where a brace or parenthesis is missing.

Last updated 2026-12-03.