• Register
0 votes
849 views

Problem :

I am trying to learn Java, I am very newbie however I wanted to know what I do to get the "run" it. I am facing below error:

Static Error: This class does not have a static void main method accepting String[].

Below is my code:

public class MyCaneirinho {
    public static void mycontar() {
        int j= 1;
        String x = " Carneirinho",
            y = " pulando a cerca.",
           z = "s";
        for (j = 1; j <= 100;j++) {
            if (j == 1) {
                System.out.println(j + x + y);
            } else {     
                System.out.println(j + x + y + z);
            }
        }
    }
} // MyCarneirinho

 

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Any Java class that you want to run directly must have the main method, it is the entry point, i.e., the program starts with main() when you execute the code.

public static void main(String args[])

So your correct code should be as below :

public class MyCaneirinho {
   public static void mycontar() {
        int j= 1;
        String x = " Carneirinho",
            y = " pulando a cerca.",
           z = "s";
        for (j = 1; j <= 100;j++) {
            if (j == 1) {
                System.out.println(j + x + y);
            } else {     
                System.out.println(j + x + y + z);
            }
        }
   }
public static void main(String[] args){
   mycontar(); // call mycontar() from the main method
}
} // MyCarneirinho
38.6k points