-2

I am trying to run my project, but the main class isn't being found. Whats wrong? Here's my code.

public class ArrayPrinter {
    public static void printArray(int[] arr) {
    int size = arr.length;
System.out.print("[");
    for(int i=0;i< size; i++){
        System.out.print(arr[i]);
           if(i<size-1){
         System.out.print(",");
        }
    }
    System.out.println("]");
}

}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 5
    there is no main class – ave4496 Jan 04 '17 at 16:54
  • So how do I give it one, sorry i have trouble with this @aleksv – ally guarisco Jan 04 '17 at 16:55
  • 1
    A main class has a `public static void main(String[] args)` method . – Arnaud Jan 04 '17 at 16:56
  • So does that go on top @Berger – ally guarisco Jan 04 '17 at 16:58
  • 3
    Please before doing anything else, consider going through a Java tutorial or introductory book. These questions suggests that you're trying to code without doing this most necessary first step, and continuing this way will only frustrate you. – Hovercraft Full Of Eels Jan 04 '17 at 16:59
  • Java and Javascript are completely different languages, don't specify both tags unless you are asking about both languages. – greg-449 Jan 04 '17 at 17:00
  • You even tagged the question `main-method`. It's a **method**. Called **`main`**. Like your first "Hello World" program had. If you haven't done a ["Hello World" program](https://docs.oracle.com/javase/tutorial/getStarted/application/), go back to chapter 1 of you Java guide and read it again. – Andreas Jan 04 '17 at 17:15

3 Answers3

2

You need a main method, not class. See below:

public class ArrayPrinter {

    public static void main(String[] args){
        //Sample use
        int[] arr = new int[2];
        arr[0] = 4;
        arr[1] = 2;
        printArray(arr);
    }

    public static void printArray(int[] arr) {
        int size = arr.length;
        System.out.print("[");
        for(int i = 0;i < size; i++){
            System.out.print(arr[i]);
            if (i < size-1){
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

}
RizJa
  • 1,961
  • 1
  • 21
  • 38
1

The signature of the main function always looks the same:

 public class ArrayPrinter {

    public static void main(String[] args) {
       // put code here
    }

    public static void printArray(int[] arr) {
    int size = arr.length;
System.out.print("[");
    for(int i=0;i< size; i++){
        System.out.print(arr[i]);
           if(i<size-1){
         System.out.print(",");
        }
    }
    System.out.println("]");
}

}
ave4496
  • 2,950
  • 3
  • 21
  • 48
  • If you are using eclipse you can press CTRL+space anywhere outside of the methods of your class and it will suggest creating a main method. – ave4496 Jan 11 '17 at 10:15
1

You need to have a main method in order to run a Java application.

The signature of the main method is

public static void main (String[] args)

More info can be found here

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101