Hey guys! So I'm trying to create a method that takes in a list of integers into an array and prints the elements inside the array line by line. How do I print the the elements? Here's what I have so far:

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


    }

    public static int[] printElements(int num){
       int[] array = new int[] {1,2,3,4,5};

       for (int i = 0; i < array.length; i++){
           System.out.print(array[i]);
        }
        System.out.println();

        return array;
    }

}

Recommended Answers

All 3 Replies

You need to read about method declaration syntax before you can go any further. The reason you are not getting it is because you declare it wrong. You need to know which part means what...

public static int[] printElements(int num)
  ^      ^      ^         ^         ^
  |      |      |         |         |
modifier |    return   method     argument
     modifier  type     name        list

I've read the link you posted. I've editted my code, my only problem is that how do I access the printElements method from main? Sorry, I'm still quite new to java.. still trying to get understand everything. :D

how do I access the printElements method from main

Since that method is static you can call it directly from the main() method without creating an instance of the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.