1

I created a method that fills an array with prime numbers. However, I am struggling to understand how can I return it after it has been filled to the main method to print it? Returning like this gives me an error that it cannot find such a symbol.

public static int[] fillArray(int a) {
    int[] arr = new int[a];
    int m = 0;
    for (int i = 1; m < arr.length; i++) {
        if (isPrime(i)) {
            arr[m] = i;
            m++;
        }
    }
    return arr;
}

public static void main(String[] args) {
    int a = Integer.parseInt(args[0]);
    System.out.println(arr);
}
HoRn
  • 1,458
  • 5
  • 20
  • 25
Artem
  • 23
  • 3
  • You would need to call your `fillArray` method from `main` and capture its return value. – khelwood Nov 26 '20 at 19:07
  • u need to make arr as global variable inside you class like `static int[] arr;` after that in your main you need to call `arr = fillArray(a);` before you print that array – aziz k'h Nov 26 '20 at 19:09
  • 1
    [You should study the basics](https://docs.oracle.com/javase/tutorial/). – MC Emperor Nov 26 '20 at 19:09
  • Declare an array in main method(similar to the array in fillArray method) and put the method's return data to the array in the main method then you can print it easily – B Sangappa Nov 26 '20 at 19:09
  • 1
    `arr` is *local variable* of `fillArray` method. Because of that each time method will be called new variable `arr` will be generated for each call, so accessing it from somewhere else is impossible, as we don't know if (1) method was called, so if `arr` will actually exist at that time (2) if method was called more than once, in which case which `arr` value you would want to access? Instead you should invoke a method like `fillArray(5)` and store its result array (returned array held by `arr`) in place from where it was called (here from `main` method) like `int[] primes = fillArray(5);` – Pshemo Nov 26 '20 at 19:12

2 Answers2

1

I would suggest, you can do something like below,

public static int[] fillArray(int a){
    int[] arr = new int[a];
    int m = 0;
    for (int i = 1; m < arr.length; i++){
        if (isPrime(i)){
            arr[m] = i;
            m++;
        }
    }
    return arr;
}

public static void main(String[] args) {
    int a = Integer.parseInt("5"); //Pass a hard coded value or Read it from Scanner class and pass the same as argument
    int[] arr = fillArray(a);
    System.out.println(arr); //This line not actually prints the values of array instead it prints the Object representation of the array

   // Below small piece of code will print the values of the array
    for(int val:arr){
         System.out.println(val);
     }

}
B Sangappa
  • 574
  • 4
  • 8
0
     public static int[] fillArray(int a){
        int[] arr = new int[a];
        int m = 0;
        for (int i = 1; m < arr.length; i++){
            if (isPrime(i)){
                arr[m] = i;
                m++;
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        int[] arr = fillArray(a); // This line was missing
        System.out.println(Arrays.toString(arr));
    }

Learn more about calling methods here: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Edgar Domingues
  • 930
  • 1
  • 8
  • 17
  • I believe your code will give ArrayIndexOutOfBoundsException for the line "int a = Integer.parseInt(args[0]);" as there are no values to fetch from args variable. I tested your code and it gives exception. Please correct me if I'm wrong – B Sangappa Nov 26 '20 at 19:15
  • 1
    Also, `System.out.println(arr)` doesn't give a usable result. – Spectric Nov 26 '20 at 19:17
  • 1
    Related to @Spectric's comment: [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784) – Pshemo Nov 26 '20 at 19:20