0

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

}
3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by NormR1
0

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

Edited by Taywin

0

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

Edited by laguardian

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.