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

橙三吉。 提交于 2021-02-19 04:46:08

问题


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]);
}
}

回答1:


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();
  }
}



回答2:


You can not define method as class. It should be

public static void kremalation()
{
String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
int i;
for (i=0; i<=ena.lenght; i++)
    System.out.println(ena[i]);
}



回答3:


public class AytiMain {


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

    public static void Kremalation() {// change here.

        String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
        int i;

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

    }    
}



回答4:


Two possible answers.

1) Remove public from second one if you would like to define it as class.

2) Move Kremalation inside closing brace and replace class with void and make it as static method.




回答5:


You cannot have executable code directly inside a class.. Add a method and use instance of that class to call that method..

public class Kremalation {

    public void method() {

        String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
        int i;

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

}

Now, in your main method, write: -

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



回答6:


Two approaches to solve this problem.....

1st there are 2 classes in the same file :

public class AytiMain {


    public static void main(String[] args) {

        new Kremalation().doIt();
    }

}

class Kremalation {

  public void doIt(){        // In Java Codes should be in blocks
                             // Like methods or instance initializer blocks

    String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
    int i;

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

   }

}

2nd change the class to method :

public class AytiMain {


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

    public static void Kremalation() {     // change here.

        String ena[]={"PEINAW","PEINOUSA","PETHAINW"};
        int i;

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

    }    
}


来源:https://stackoverflow.com/questions/12804598/syntax-error-on-token-expected-after-this-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!