NERO
NERO

Reputation: 1

method with int array as parameter

The task is: Write an method printArray. It shall take an int array as parameter. It shall write every int in the array on a row (system out print) If the parameter is null, nothing shall be written.

In my code I get this message: The method println(boolean) in the type PrintStream is not applicable for the arguments (void).

MY CLASS:
public class Upg9_tenta {
    public static void printArray(int arr[]){
        int i = 0;
        while(i<arr.length){
            System.out.print(arr[i]);
            i++;

    }
}
}

MY MAIN:

public class Upg9_tentamain {
    public static void main (String []args){

        int []arr = {1, 3, 8, 6};

        Upg9_tenta.printArray(arr);

        System.out.println(Upg9_tenta.printArray(arr));


}
}

Upvotes: 0

Views: 568

Answers (3)

atish shimpi
atish shimpi

Reputation: 5023

println() method accepts the parameters, those parameters method will print on console. In your case you are calling printArray() method is void method. reference for println() method

It is returning void, you have to either change return type of printArray() method or remove Upg9_tenta.printArray(arr) from System.out.println() method.

Modified Code :

 public class Upg9_tentamain {
        public static void main (String []args){

            int []arr = {1, 3, 8, 6};

            Upg9_tenta.printArray(arr);  // just call method to print array

            //Upg9_tenta.printArray(arr);         

     }
    }

Upvotes: 0

August
August

Reputation: 12558

printArray() is a void method, meaning that it has no return value. You can't pass something with no value to another method.

You already have a line that calls printArray() correctly, so remove this line

System.out.println(Upg9_tenta.printArray(arr));

Upvotes: 0

Makoto
Makoto

Reputation: 106410

printArray returns void, so you can't actually print out its return value.

Just remove the line that calls System.out.println for that method, and you should be okay.

Upvotes: 3

Related Questions