Java ';' expected Error in AP CSA: Missing Semicolons
Java "';' expected" Error: Missing Semicolons and Braces (AP CSA)
';' expected means the compiler reached the end of what should be a statement but did not find the semicolon that ends it. It is one of the most common beginner compiler errors, and the fix is usually a single missing semicolon, often on the line above the one reported.
Error type: Compiler error (the code will not build).
Plain meaning: A statement is missing its terminating semicolon.
Fastest fix: Add the missing ; at the end of the statement, checking the line just before the one the compiler names.
Why students see this error
Every simple statement in Java ends with a semicolon. The compiler uses semicolons to know where one statement stops and the next begins. When it parses a statement and reaches the next token without finding the expected ;, it reports ';' expected. Because the compiler only notices once it reads too far, the error is often reported on the line after the one actually missing the semicolon.
- Forgetting the semicolon at the end of a statement.
- A line break that makes it look like a statement ended when it did not.
- Confusing places that do not take semicolons (like the end of an
if (...)header or a method signature) with ones that do.
Broken code that triggers it
Throws ';' expectedpublic class Total { public static void main(String[] args) { int a = 5; int b = 7 // missing semicolon here int sum = a + b; System.out.println(sum); } }
The line int b = 7 has no terminating semicolon. The compiler keeps reading into the next line looking for the end of the statement, then reports ';' expected because it found the keyword int where a semicolon should have been.
Trace: watch where it breaks
The compiler scans for the end of each statement:
| Statement | Ends with ;? | Result |
|---|---|---|
| int a = 5; | yes | ok |
| int b = 7 | no | ';' expected |
| int sum = a + b; | (not reached) | - |
The error may be reported on the int sum line because that is where the compiler finally noticed; the missing semicolon is on the line above.
The fix
Runs correctlypublic class Total { public static void main(String[] args) { int a = 5; int b = 7; // semicolon added int sum = a + b; System.out.println(sum); } }
Adding the semicolon ends the statement and the code compiles. A useful habit: when you see ';' expected on a line that clearly has its semicolon, check the line directly above it.
How College Board tests this concept
AP Connection (Official CED)
Writing syntactically correct statements is assumed throughout the course. While the exam will not ask about semicolons directly, fluent reading and writing of well-formed Java is necessary for both MCQ tracing and free-response code.
Read it in the official CED: Unit 1 (CED p.23) · Unit 2 (CED p.53)
Note which constructs do NOT take a semicolon: the header of an if, for, while, or a method signature followed by a block. Adding a stray semicolon there causes different bugs, such as an empty loop body, which the exam does test.
Common MCQ traps (practice set)
Watch out
The trap is two-sided. Forgetting a semicolon where one is needed gives ';' expected. But adding a semicolon after an if (...) or for (...) header compiles fine and silently creates an empty body, which is a logic bug the exam loves.
Question 1: Where is the missing semicolon?
1: int x = 10; 2: int y = 20 3: int z = x + y;
The compiler reports ';' expected on line 3. Which line most likely has the actual problem?
- A) Line 1
- B) Line 2
- C) Line 3
- D) No line; it is a runtime error
int y = 20) is missing its semicolon. The compiler does not notice until it reads int on line 3, so it reports the error there, but the fix belongs on line 2.Question 2: Which need a semicolon?
Consider where a semicolon belongs.
I. int count = 0 II. if (count > 0) III. System.out.println(count)
Which lines should end with a semicolon?
- A) I and III
- B) I only
- C) II and III
- D) I, II, and III
if header followed by a block or statement; putting a semicolon right after if (count > 0) would wrongly create an empty body. So I and III.Question 3: Spot the silent bug
This compiles but behaves unexpectedly.
for (int i = 0; i < 3; i++);
{
System.out.println(i);
}What does the stray semicolon do here?
- A) Prints 0 1 2
- B) ';' expected compile error
- C) The loop body is empty; the block runs once and i is undefined
- D) Infinite loop
for header makes the loop body empty. The block below runs once, separately, and i is out of scope there, causing a different error. This is the silent-semicolon trap, the opposite of a missing one.Related help pages
Frequently asked questions
Why is the error on a line that has its semicolon?
The compiler only notices the missing semicolon when it reads into the next line. The line it names has its semicolon; the line above does not.
Do if and for headers need semicolons?
No. The header is followed by a statement or block, not terminated by a semicolon. A semicolon right after the header creates an empty body, which is a silent logic bug.
Is this a runtime error?
No. It is a compile error; the code will not build until the statement is properly terminated.
Master the concepts behind the error
Work through the full AP CSA lessons, then test yourself.
AP CSA Lessons All Java ErrorsGet in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]