Java course

Finding and fixing Java errors

1.1.2 — Finding and fixing Java errors

Learn to read Java error messages and fix common mistakes. All examples are included here.

System.out.println("Hello"); prints text and moves to a new line. The text needs two double quotes, the call needs two parentheses, and the statement ends with ;. Java names are case-sensitive.

Source, compilation, and execution

Source is the Java code you write. Compilation checks that code and translates it into instructions. Execution runs those instructions.

If compilation fails, that code does not run. An error message describes a problem found in the source.

Successful compilation means the code follows Java rules. You still need to check that its output is what you intended.

Read an error message

  1. Start with the first error. Later errors may be caused by it.
  2. Find the name or punctuation it mentions. Check the nearby source too.
  3. Fix one problem and run the cell again.
  4. Check the output once it runs.

The exact wording can vary between compilers. Use the message to locate the problem.

Case-sensitive names

system.out.println("Hello!");

This is incorrect: Java knows System, with an uppercase S. system is a different name. An error such as “cannot find symbol” means a referenced name could not be found.

In the working cell below, change System to system and run it. Read the error, then fix the name to System. You can edit and rerun a failed cell.

Missing quotes

System.out.println("Hello, world!);

The closing double quote is missing. Java may report an “unclosed string literal”: it cannot find the end of the text.

The correct ending is !");: close the String, close the call, then end the statement.

Remove the closing quote in the next cell, run it, and inspect the error. Add the missing quote to fix the error, then rerun the cell.

Missing semicolons

System.out.println("First line")
System.out.println("Second line");

The first statement is missing ;. A line break does not replace a semicolon. The compiler may point to the end of the first line or the beginning of the second.

Remove the first semicolon in the next cell and run it. Add the missing semicolon to fix the error, then check that both lines print.

Spaces and spelling

These calls behave the same way:

System.out.println("Ready");
System.out.println ( "Ready" );

Spaces around punctuation are allowed here. A space inside a name, such as print ln, changes the source and is invalid. Keep the spelling of each name intact.

Valid code can produce the wrong result

Suppose you want to print Sum: 30.

+ adds two numbers, but joins text when one operand is a String. Joining text is called concatenation. These + operations group from left to right.

In the next cell, "Sum: " + 18 produces "Sum: 18". Joining 12 to that text produces "Sum: 1812". Run the cell to check.

This is a logic error: the code runs, but its result is wrong for the task. The compiler has no reason to reject valid text concatenation.

Group 18 + 12 in parentheses to calculate the sum before joining it to the label.

Practice: fix three statements

Replace the workspace with corrected versions of these statements:

System.out.printLn("Hello");
System.out.println("Java);
System.out.println("Ready")

The expected output is three lines: Hello, Java, and Ready. Check method spelling, closing quotes, and semicolons.

Hints

The method name is println, all lowercase. A String needs a closing double quote before ). The statement ends with ;.

Worked solution
System.out.println("Hello");
System.out.println("Java");
System.out.println("Ready");

Run your corrected code and compare all three output lines.

Practice: fix a calculation

This statement runs but prints Sum: 2716:

System.out.println("Sum: " + 27 + 16);

Change it to add the numbers before joining the result to the label. Then change 16 to 19 and run it again.

This requires changing the calculation, rather than fixing invalid syntax.

Check your understanding

Explain how a compilation error differs from a logic error. Give one example of each from this lesson.

Enable JavaScript to run the Java notebook.