Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Paramaters for Method type (int[] nums)

I am working on a code that adds numbers in an array together and makes the sum. However, if a number is 13, that number and the number after it are skipped. I cannot get my main() method to input parameters into the method that I created to do this task however.
The syntax I have currently is:

public static int sum13(int[] nums) {
     int sum = 0;
     for (int i = 0; i < nums.length; i++) {
          if (nums[i] ==13 || nums[i]-- == 13) {
               continue;
          } else {
               sum += nums[i];
          }
     return sum;
     }
}

public static void main(String[] args) {
     System.out.println(sum13([1, 2, 2, 1]));
     System.out.println(sum13([13, 1, 2, 13, 3, 3]));
}

}

On the System.out.println lines, I am getting the error message,

The method sum13(int[]) in the type A3 is not applicable for the arguments (int, int, int, int)

Does anyone know how to resolve this error?

like image 549
Michael Debo Avatar asked Jul 06 '21 06:07

Michael Debo


People also ask

What is int * NUMS?

int nums[] = {1,2,3,4}; is a valid declaration where you are saying nums is an array of type int and memory is allocated based on number of elements passed during initialization.

What is Java method parameters?

Parameters and Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

How do you call a method with parameters from the same class in Java?

There are two ways to call a method with parameters in java: Passing parameters of primtive data type and Passing parameters of reference data type. 4. in Java, Everything is passed by value whether it is reference data type or primitive data type.


1 Answers

Options:

1 - create an array, since the method is expecting one

sum13(new int[] { 1, 2, 2, 1};
// or
int[] array = { 1, 2, 2, 1}; // short for int[] array = new int[] { 1, 2, 2, 1};
sum13(array);

2 - use varargs parameter (variable arity parameter) like:

public static int sum13(int... nums) {  // creates: int[] nums

called with:

sum13(1, 2, 2, 1)

in this case Java will create the array and pass it as int[] nums.

The ... can be used with any type, but it must be the last parameter specified in the method declaration. The compiler will generate the method as if an array is passed - so it is used inside the method as an array of the specified type; the array itself is created when the method is called. Optionally an array can be given instead of the values.


Despite not the (direct) question, be warned that nums[i]-- == 13 is not doing what probably was intended. The Postfix Decrement Expression X--, where X is a variable (or expression denoting one), will decrement the variable and return its value before it was decremented. So nums[i]-- will return the value of nums[i] and decrement the value stored at index i. Probably it was intended to be nums[i-1] == 13, but that will result in an exception when i == 0 (unless not evaluated).

like image 145
user16320675 Avatar answered Oct 19 '22 22:10

user16320675