Home » Core Java » How to Fix the Identifier Expected Warning in Java

About Kevin Anderson

Kevin has been tinkering with computers for longer than he cares to remember.

How to Fix the Identifier Expected Warning in Java

In this article, we’ll give you some pointers on how to fix the Identifier Expected Warning in Java.

1. Why does it appear?

Identifier Expected is one of many different syntax error messages a Java compiler may produce. It occurs when the compiler reaches a point in your program where, based on the grammar of the Java language, an identifier must appear, but something else is there instead.

Identifier Expected

2. What does the Identifier Expected Warning in Java mean?

Technically, an “Identifier Expected” error means exactly what it says: at some point in your program, the Java compiler expected to find an identifier, but instead found something else. However, Java compilers and Java developers each view code (especially buggy code) through very different sets of eyes. What a Java compiler might diagnose as “Error A at location x” can look more like “Error B at location y” to a human observer. So, in practice, it’s best not to take “<identifier> expected” errors too literally: treat them as if they mean “an error”, rather than “the error”.

3. How to fix the Identifier Expected Warning in Java

The key to addressing “<identifier> expected” errors successfully is not to read too much into them. Don’t assume the problem is literally a missing identifier at the indicated location, and don’t assume the solution is to insert an identifier at the indicated location. Always look at the bigger picture, and come to your own conclusion about what the “real” problem and its proper solution are. Here are a couple of examples to inspire you.

3.1 Example #1

These two nearly identical pieces of code each have an error at line #5:

Demo1WithErrors.java
package com.jcg.identexpected;

public class Demo1WithErrors
{
    public static double squareOf(double)
    {
        return x * x;
    }
}
code\demos\src\main\java\com\jcg\identexpected\Demo1WithErrors.java:5: error:  expected
     public static double squareOf(double)
                                         ^
 1 error
Demo2WithErros.java
package com.jcg.identexpected;

public class Demo2WithErrors
{
    public static double squareOf(x){
        return x * x;
    }    
}
code\demos\src\main\java\com\jcg\identexpected\Demo2WithErrors.java:5: error:  expected
     public static double squareOf(x){
                                    ^
 1 error

The Java compiler diagnosed identical errors in both cases: an <identifier> was expected at the location of the right-paren. You, however, probably see two somewhat different problems:

  • In Demo1WithErrors, the parameter was supposed to be double x; the type double was specified, but the name x was omitted;
  • In Demo2WithErrors, the parameter was supposed to be double x; the name x is present, but the type double was omitted.

But had you been thinking like a Java compiler, you would have seen things this way:

  • In Demo1WithErrors, the parameter should consist of a <type>, followed by an <identifier>; the <type> is double, but no <identifier> follows, only a right-paren. Thus “<identifier> expected” error at the position of the right-paren!
  • In Demo2WithErrors, the parameter should consist of a <type> followed by an <identifier>; the type is x, but no <identifier> follows, only a right-paren. Thus, “<identifier> expected” error at the position of the right-paren.

Both sets of assessments are technically correct, just from different points of view.

The fix, in both cases, is to make the parameter declaration read double x. In the case of Demo1WithErrors, it’s a simple matter of taking the error message more or less at its word and inserting the missing identifier x after the existing type double (in other words, at the position right-paren):Demo1.java

package com.jcg.identexpected;

public class Demo1
{
    public static double squareOf(double x)
    {
        return x * x;
    }
}

As for Demo2WithErrors, the “intuitive” fix is simply to insert the missing type double before the existing parameter name x, more or less ignoring the specifics of the “<identifier> expected” error. But another way to think about it is that you are first inserting the missing identifier, x, at the location of the right-paren, and then correcting the already-present, but incorrect, type x to double. Either way, the end result is:Demo2.java

package com.jcg.identexpected;

public class Demo2
{
    public static double squareOf(double x){
        return x * x;
    }    
}

3.2 Example #2

An “<identifier> expected” error can sometimes be just a minor symptom of a much larger problem. Consider this common newbie mistake:Demo3WithErrors.java

package com.jcg.identexpected;

import java.util.Arrays;

public class Demo3WithErrors
{
    int[] nums = {9,1,3,10,7,4,6,2,8,5};
    int max;
    max = nums[0];
    for (int i = 1; i < nums.length; ++i){
        if (nums[i] > max){
            max = nums[i];
        }    
    }
    System.out.println("List: " + Arrays.toString(nums));
    System.out.println("Largest = " + max);
}

This code produces a rather impressive slew of error messages (29 in all!) that starts off with these:

code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:9: error:  expected
     max = nums[0];
        ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error: illegal start of type
     for (int i = 1; i < nums.length; ++i){
     ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error: ')' expected
     for (int i = 1; i < nums.length; ++i){
               ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error: illegal start of type
     for (int i = 1; i < nums.length; ++i){
                  ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error:  expected
     for (int i = 1; i < nums.length; ++i){
                   ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error: ';' expected
     for (int i = 1; i < nums.length; ++i){
                    ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error: > expected
     for (int i = 1; i < nums.length; ++i){
                             ^
 code\demos\src\main\java\com\jcg\identexpected\Demo3WithErrors.java:10: error: '(' expected
     for (int i = 1; i < nums.length; ++i){

Clearly there's something more going on here than a simple missing identifier. The Java compiler seems unable to recognize perfectly normal Java statements!

The problem here is that these statements have been dropped right into the top level of the Demo3WithErrors class body, where only class member declarations belong. The compiler doesn't recognize statements at this point in the code, simply because it isn't expecting any statements. Instead, it tries to parse the statements as class member declarations, with varying degrees of success.

The solution, of course, is to put those statements where they belong, in an appropriate context. Here it makes sense to move them into a new main method:Demo3.java

package com.jcg.identexpected;

import java.util.Arrays;

public class Demo3
{
    public static void main(String[] args)
    {
        int[] nums = {9, 1, 3, 10, 7, 4, 6, 2, 8, 5};
        int max;
        max = nums[0];
        for (int i = 1; i < nums.length; ++i) {
            if (nums[i] > max) {
                max = nums[i];
            }
        }
        System.out.println("List: " + Arrays.toString(nums));
        System.out.println("Largest = " + max);
    }
}

4. Summary

That was an article on how to fix the Identifier Expected warning in Java.

  • The "Identifier Expected" message is caused by a syntax error in your code;
  • The "real" error might, or might not, actually be the result of a missing identifier, and inserting the supposed missing identifier might, or might not, fix it;
  • Look at the bigger picture and use your own best judgment.

5. Download the Source Code

Use the link below to download a Maven project containing all the example code from this article.

Download
You can download the full source code of this example here: How to Fix the Identifier Expected Warning in Java

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments