Java: Identifier Expected

Java: Identifier expected

Put your code in a method.

Try this:

public class MyClass {
public static void main(String[] args) {
UserInput input = new UserInput();
input.name();
}
}

Then "run" the class from your IDE

identifier expected in Java

You forgot the try keyword. It should be

try {
// statements
} catch (IOException ioe) {
// handle exception
}

And don't forget to put your code inside a method.

java identifier expected error in main function

The

...normalize.(Entropy.charCount...
// ^

part is wrong. You can't have a parentheses right after a member access operator. . is looking for method name on its right, but is finding a parenthesis instead. To call normalize, just get rid of the ..

Why wouldn't this code run in Java? Eror : identifier expected

So why is the compiler rejecting this code?

Because the code is syntactically invalid as Java, and the Java compiler is required to reject code that is invalid.

The Java language specification says that the only things that can be immediately nested in a class are member declarations or initializer blocks. Member declarations are:

  • field declarations
  • nested classes, interfaces, enums, etcetera
  • methods
  • constructors

Initializer blocks are either static initializer blocks or instance initializer blocks.

A typical statement (like the one you are trying to insert there) is neither a member declaration or a block.


The relevant section of the Java Language specification is JLS 8.1.6



Related Topics



Leave a reply



Submit