Reputation: 133
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]);
}
}
Upvotes: 3
Views: 39680
Reputation: 33534
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]);
}
}
Upvotes: 0
Reputation: 20112
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]);
}
}
Upvotes: 1
Reputation: 213223
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();
}
Upvotes: 0
Reputation: 66637
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.
Upvotes: 0
Reputation: 133567
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();
}
}
Upvotes: 6
Reputation: 19185
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]);
}
Upvotes: 3