"Hey, young recruit, who makes a bunch of mistakes every day, even in the simplest programs!

"Um... Hi, Diego. Boy, you really know how to cheer people up!"

"Of course, I do! Learn how to say what needs to be said. Like I said, 'every day'. Putting it more politely, my greeting means "future successful programmer, if, of course, you doesn't blow it."

"Oooh, well then thank you, my most metallic teacher!.

"You'll thank me after this lesson. Here I'm going to list the most popular errors noted by the compiler. And your job is to remember. Forewarned is forearmed.

Forgetting a semicolon

"The most common mistake aspiring Java programmers make involves the semicolon. Or rather, its absence where it should be."

"The truth is... I've committed this offense repeatedly."

"Every statement inside a method must end with a semicolon. The semicolon is what separates statements or commands: this is how we tell the Java compiler where one command ends and the next begins.

Examples of errors:

Incorrect code Correct code
int a
int b = 5
int c = a + b
int a;
int b = 5;
int c = a + b;
System.out.println("Hello")
System.out.println("Hello");
if (2 > 3)
   System.out.println("Are we in Australia?")
if (2 > 3)
   System.out.println("Are we in Australia?");
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Period and comma
Periods and commas are important for more than just punctuation in ordinary text. Fix mistakes in the code, so that the program compiles and works correctly.

Forgetting to close quotes

"The second most common mistake for newcomers to Java is writing a string in the code and then forgetting to close the quote.

Every string literal in the code must be enclosed on both sides with double quotation marks ("). Beginner programmers very often put quotation marks at the beginning of text, but they forget to close them at the end.

Incorrect code Correct code
String s = "Hello;
String s = "Hello";
System.out.println("Hello);
System.out.println("Hello");
String s = "Hello";
String message = s + " and by. ;
String s = "Hello";
String message = s + " and bye.";
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Oh, these damnable quotation marks
The code already has quotation marks, but as it turns out, there are not enough of them. Fix mistakes in the code, so that the program compiles and works correctly.

Forgetting to include a plus sign when gluing together strings

"Another common mistake when working with strings is forgetting to write a plus sign when gluing strings together. This error is especially prevalent when text and variables are concatenated in a long expression in code.

Here are some examples:

Incorrect code Correct code
String s = "Hello";
String message = s  " and bye.";
String s = "Hello";
String message = s + " and bye.";
int age = 35;
System.out.println("Age=" age);
int age = 35;
System.out.println("Age=" + age);
int age = 35;
System.out.println("Age=", age);
int age = 35;
System.out.println("Age=" + age);
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Con, Cat, Enation
"Con", "cat", "enation". How would you merge these three strings? Do you remember how to concatenate Strings in Java? Fix mistakes in the code, so that the program compiles and works correctly.

Forgetting to close curly braces

"This is a very common mistake. There are two situations where this is typical:

  1. You decided to copy code from somewhere and accidentally missed some curly braces.
  2. You simply aren't troubling yourself to make sure that every open parenthesis is matched by a closing parenthesis."

"The second option is what I do. Sometimes I get so carried away that I forget!".

"To avoid these errors, it is usually recommended for beginner programmers to write the closing curly brace underneath the opening one.

Examples:

Incorrect code Correct code
if (2 < 3)
{
   if (3 < 4)
   {
      System.out.println("Mathematics!");
   }
if (2 < 3)
{
   if (3 < 4)
   {
      System.out.println("Mathematics!");
   }
}
{
   if (2 < 3)
   {
      if (3 < 4)
      {
         System.out.println("Mathematics!");
      }
   }
{
   if (2 < 3)
   {
      if (3 < 4)
      {
         System.out.println("Mathematics!");
      }
   }
}
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Keeping track of parentheses
It seems we have an extra parenthesis? Or is one missing? Deal with the problem.

Forgetting to add parentheses

"Most often this mistake is made by developers who know programming languages that don't require parentheses in similar situations.

One possibility is that they simply forget to put parentheses at the end of a method call:

Another possibility is that they forget to wrap the condition of an if-else statement in parentheses.

Examples:

Incorrect code Correct code
System.out.println("Hello!");
System.out.println;
System.out.println("And bye!");
System.out.println("Hello!");
System.out.println();
System.out.println("And bye!");
if 2 < 3
{
   if 3 < 4
   {
      System.out.println("Mathematics!");
   }
}
if (2 < 3)
{
   if (3 < 4)
   {
      System.out.println("Mathematics!");
   }
}
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
And parentheses once again
It seems that once again the problem is with the parentheses. But with different ones this time. Fix mistakes in the code, so that the program compiles and works correctly.

Writing the main method declaration incorrectly

"As soon as they declare the bloody main method! There is probably nothing that trips up newbies as much as this poor method. Importantly, then they are always surprised and wonder why their program won't start? And, of course, the programmer isn't to blame, but the program, the compiler, the code validator, the Java machine, etc. The list of scapegoats is endless.

Examples:

Incorrect code Explanation
static void main(String[] args)
public is missing
public void main(String[] args)
static is missing
public main(String[] args)
void is missing
void main(String[] args)
public and static are missing
public static void main(String args)
[] is missing
public static void main()
String[] args is missing
public static int main(String args)
We have int instead of void
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
The long-suffering main method
Make one fix in the code to get the program to run.

The file name is different from the class name

"According to the Java standard, all Java classes must be stored in a file with the same name as the class name. And as mentioned earlier, the case of the letters matters here:

File name Class name Note
Solution.java Solution Everything is fine
Solutions.java Solution The file name as a superfluous letter 's'
solution.java Solution The file name starts with a lowercase letter
Solution.txt Solution The file extension is .txt instead of .java
Solution.java solution The class name starts with a lowercase letter

"Actually, several classes can be declared in a file with the .java extension, but only one of them can have the word public before the class name. And this is the name that must match the file name.

"A .java file must always have a class whose name is the same as the file name, and that class needs to have the public modifier. Example:

Solution.java
public class Solution
{
}

class Apple
{
}

class Pineapple
{
}

"Additionally, the Java language lets you write classes within classes. This is another way to get around the above limitation. If a public class (a class with the public modifier) is declared in a file and has the same name as the file name, then you can declare as many classes as you like inside this public class. That said, these will no longer be ordinary classes. Instead, they will be internal or nested classes. Example:

Solution.java
public class Solution
{
   public class Apple
   {
   }

   public static class Pineapple
   {
   }
}
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Correct class
Fix mistakes in the code, so that the program compiles and works correctly.

Forgetting to write package

"Since programs usually have thousands of classes, it can be difficult to find simple, understandable, and unique names for all of them. That's why in Java it is customary to group classes into packages using the package keyword. Exactly the way the files are grouped into folders."

"Ah-ha, so that's why each class should begin with an indication of the package that it belongs to."

"Exactly. Here's an example:

Code without package Corrected example
public class Solution
{
}
package en.codegym.tasks.task0001;

public class Solution
{
}
undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Need a package?
Fix a mistake in the code, so that the program compiles and works correctly.

Forgetting to add import

"If we want to use somebody else's class in our program, we have two options: everywhere in our code we must also write its package name before the name of the class. Alternatively, we can write the fully qualified class name with the import keyword once. Example:

Without using import Using import
public class Solution
{
   java.util.Scanner scanner = new java.util.Scanner();
}
import java.util.Scanner;

public class Solution
{
   Scanner console = new Scanner();
}

"Both options work, but if you simply write Scanner in your code without adding import, then the compiler will not be able to understand which package it needs to take the Scanner class from, and your program won't compile."

"Thank you, Diego. This lesson will make me pay more attention."

"That's what I was counting on. Good luck!"

undefined
11
Task
New Java Syntax, level 11, lesson 5
Locked
Imported code
Fix a mistake in the code, so that the program compiles and works correctly.