Blog |

How to Handle the <Identifier> Expected Error in Java

How to Handle the <Identifier> Expected Error in Java
Table of Contents

Introduction to Identifiers

By definition, an identifier in Java is a sequence of one or more characters, where the first character must be a valid first character (letter, $, _) and each subsequent character in the sequence must be a valid non-first character (letter, digit, $, _). An identifier can be used to name a package, a class, an interface, a method, a variable, etc. An identifier may contain letters and digits from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese, Japanese, and Korean. This allows programmers to use identifiers in programs written in their native languages [1].

 

Identifier Expected Error: What It Is & What Triggers It

The initial phase of the Java compilation process involves lexical analysis of the source code. The compiler reads the input code as a stream of characters and categorizes them into lexemes of tokens, before proceeding to parse the tokens into a syntax tree. Here is where all tokens, including identifiers, are being checked against a predefined set of grammar rules. When the compiler reaches a point where, according to these rules, an identifier is expected to appear but something else is found instead, it raises the <identifier> expected error, where the angle brackets denote a reference to a token object [2].

The <identifier> expected error is a very common Java compile-time error faced by novice programmers and people starting to learn the language. This error typically occurs when an expression statement (as defined in [3]) is written outside of a constructor, method, or an instance initialization block. Another common scenario for this error is when a method parameter does not have its data type, or similarly, its name declared.

 

Identifier Expected Error Examples

 

Misplaced expression statements

 

When isolated expression statements such as assignments or method invocations appear outside the scope of a constructor, a method, or an instance initialization block, the <identifier> expected error is raised (Fig. 1(a)). Moving the statements in question to an appropriate place resolves this error (Fig. 1(b)).

(a)

package rollbar;

public class IdentifierExpectedExpression {
  private String str;
  str = "Rollbar";
  System.out.println(str);
}
IdentifierExpectedExpression.java:5: error: <identifier> expected
  str = "Rollbar";
     ^
IdentifierExpectedExpression.java:6: error: <identifier> expected
  System.out.println(str);
                    ^
IdentifierExpectedExpression.java:6: error: <identifier> expected
  System.out.println(str);
                        ^
3 errors

(b)

package rollbar;

public class IdentifierExpectedExpression {
 private String str;

 public IdentifierExpectedExpression(String str) {
   this.str = str;
 }

 public static void main(String... args) {
   var rollbar = new IdentifierExpectedExpression("Rollbar");
   System.out.println(rollbar.str);
 }
}
Rollbar

Figure 1: <Identifier> expected on misplaced expression statement (a) errors and (b) resolution

 

Misplaced declaration statements

One interesting but not so obvious example of where the <identifier> expected error might appear is the try-with-resources statement [4]. This statement requires any closeable resource (such as a BufferedReader instance) to be declared within parentheses immediately after the try keyword, so it can be closed and finalized automatically. Declaring a resource variable outside the try-with-resources statement will raise the <identifier> expected error, as shown in Fig 2.

(a)

package rollbar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IdentifierExpectedDeclaration {
  public static void main(String... args) {
    StringBuilder result = new StringBuilder();
    BufferedReader br = null;

    try (br = new BufferedReader(new InputStreamReader(System.in))){
      String line = "";
      while (!(line = br.readLine()).isBlank()) {
        result.append(line);
      }
    } catch(IOException e){
      e.printStackTrace();
    }

    System.out.println(result);
  }
}
IdentifierExpectedDeclaration.java:12: error: <identifier> expected
        try (br = new BufferedReader(new InputStreamReader(System.in))) {
               ^
1 error

(b)

package rollbar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IdentifierExpectedDeclaration {
  public static void main(String... args) {
    StringBuilder result = new StringBuilder();

    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
      String line = "";
      while (!(line = br.readLine()).isBlank()) {
        result.append(line);
      }
    } catch(IOException e){
      e.printStackTrace();
    }

    System.out.println(result);
  }
}

Figure 2: <Identifier> expected on misplaced declaration statement (a) error and (b) resolution

 

Missing method parameter data type or name

A method parameter should consist of a data type, followed by it’s name, which is an identifier. Being a statically typed language with strict grammar rules, Java treats these as crucial pieces of information—omitting either one will inevitably raise the <identifier> expected error.

In the toAbsoluteValue method in Fig. 3(a), the type of the parameter is double, but no identifier follows, only a right parenthesis. Therefore, the <identifier> expected error is raised at the position of the right parenthesis. In Fig. 3(b) the compiler assumes the parameter type to be x, but it sees no identifier next to it, hence halting with the same error.

(a)

package rollbar;

public class IdentifierExpectedMethodParams {

  public static double toAbsoluteValue(x) {
    return x < 0 ? x * -1 : x;
  }

  public static void main(String... args) {
    System.out.println(toAbsoluteValue(-4.3));
  }
}
IdentifierExpectedMethodParams.java:5: error: <identifier> expected
  public static double toAbsoluteValue(x) {
                                        ^
1 error

(b)

package rollbar;

public class IdentifierExpectedMethodParams {

  public static double toAbsoluteValue(double) {
    return x < 0 ? x * (-1) : x;
  }

  public static void main(String... args) {
    System.out.println(toAbsoluteValue(-4.3));
  }
}
IdentifierExpectedMethodParams.java:5: error: <identifier> expected
  public static double toAbsoluteValue(double) {
                                             ^
1 error

(c)

package rollbar;

public class IdentifierExpectedMethodParams {

  public static double toAbsoluteValue(double x) {
    return x < 0 ? x * -1 : x;
  }

  public static void main(String... args) {
    System.out.println(toAbsoluteValue(-4.3));
  }
}
4.3

Figure 3: <Identifier> expected on missing method parameter (a) data type, (b) name, (c) resolved by specifying both the data type and the name of the parameter

 

Summary

Identifiers are used to name structural units of code in Java. A compile-time error associated with identifiers and common amongst Java newcomers is the <identifier> expected error. When the Java compiler expects to find an identifier but discovers something else in its place, the compilation process fails by triggering the <identifier> expected error. With the aim to learn how to comprehend, resolve, and prevent this error, relevant examples have been presented in this article.

 

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

 

References

[1] Oracle, 2021. The Java® Language Specification. Chapter 3. Lexical Structure. Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.8 . [Accessed Nov. 15, 2021].

[2] A. Reis, Compiler Construction Using Java, JavaCC, and Yacc. Hoboken, New Jersey: John Wiley & Sons, 2012, pp. 355-358.

[3] Oracle, 2021. Expressions, Statements, and Blocks (The Java™ Tutorials > Learning the Java Language > Language Basics). Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html. [Accessed Nov. 15, 2021].

[4] Oracle, 2021. The try-with-resources Statement (The Java™ Tutorials > Essential Java Classes > Exceptions). Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html . [Accessed Nov. 15, 2021].

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape