Skip to content
Advertisement

Syntax error on token “;”, “{” expected after this token

Just a simple class calls a class that prints an array. I get a syntax error in Eclipse. I also get an error that I don’t have a method called Kremalation.

public class AytiMain {

    public static void main(String[] args) {
        AytiMain.Kremalation();
    }
}

public class Kremalation {

    String[] ena = { "PEINAW", "PEINOUSA", "PETHAINW" };
    int i; // <= syntax error on token ";", { expected after this token

    for (i = 0; i <= ena.lenght; i++)
        System.out.println(ena[i]);
}
}

Advertisement

Answer

You have code (which is not declaring a variable and/or initializing it) ouside a method, which is:

for (i=0; i<=ena.lenght; i++)
    System.out.println(ena[i]);

In Java, code MUST reside inside a method. You can’t call a class, you have to call a method that is declared inside a class.

WRONG:

class ClassName {
   for (...) 
}

CORRECT:

class ClassName {
  static void method() {
    for (...)
  }

  public static void main(String[] args) {
    ClassName.method();
  }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement