Reputation: 9
I am trying to print out a simple code but I do not seem to be able to pass the array variable of the method. Sorry if it is something obvious, I am only starting with Java. I am getting the "The method asd in the type MyClass is not applicable for the arguments (int,int,int,int)
public int asd(int[] nums) {
int count = 0;
// Note: iterate to length-1, so can use i+1 in the loop
for (int i=0; i < (nums.length-1); i++) {
if (nums[i] == 6) {
if (nums[i+1] == 6 || nums[i+1] == 7) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(asd(1,22,3,4,2,2,2));
}
Upvotes: 0
Views: 513
Reputation: 4878
you can try this
public static void main(String[] args) {
int []arr={1,22,3,4,2,2,2}; //this is array literal
System.out.println(asd(arr));
}
The above program will give compilation error attaching screenshot below
Upvotes: -1
Reputation:
The method asd
is expecting a single parameter of type int []
. You are attempting to pass each integer as a separate parameter (resulting in seven parameters , 1,22,3,4,2,2,2
).
To correct the compiler-error you can enclose the parameter values in {}
and prefix it with new int[]
to create an Array literal:
System.out.println(asd(new int[] {1,22,3,4,2,2,2}));
Upvotes: 5