3

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
  • 1
    Well,you don't have a method Kremalation, you have a class Kremalation (which by the way has code outside of any method). Commented Oct 9, 2012 at 16:42

6 Answers 6

6

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();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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

    }    
}

Comments

0

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.

3 Comments

That wouldn't work because the method would need a return type.
@MattLeidholm: Which method? Sorry! I see what you mean.
After making the Kremalation "class" into a static method, it would still need a return type (or "void"). I see you just edited your answer to include that. Nevermind.
0

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

Comments

0

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

    }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.