Method Paramaters for Method type (int[] nums)

Viewed 343

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?

5 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).

The struct of this question is very good! The reason for this question is that the parameter of the main method is not applicable for the sum13 method. About the understood of array is a little wrong. The right solution is

System.out.println(sum13(new int[]{1,2,3,4}));

You can use a simple if-else to skip the number if it is 13 and the number next to it:

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

Here, if the number is 13, you just increment i by 1. If the number at the current iteration is 13, it will not add to the sum because the sum is in the else part, also, it will not add the number right next to it, because you increment i by 1, and at the end of the loop i is incremented again which means i will effectively be incremented twice, skipping the number next to 13 if the current number is 13.

Also, I have used variable length arguments as mentioned in the other answers to allow you to pass an arbitrary number of parameters to sum13 and therefore you call it like this:

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

Some of the mistakes in your code are-

  1. Return statement inside a for loop.

  2. Wrong declaring and passing the array to the sum13 method.

  3. nums[i]-- == 13 is not the correct way to look back for 'i-1'th element and when looking for the i-1 the element do check it does not goes below zero else you will face ArrayIndexOutOfBounds exception.

    public static int sum13(int[] nums) {
     int sum = 0;
     for (int i = 0; i < nums.length; i++) {
          if (nums[i] == 13 || ( i > 0 && nums[i-1] == 13)) {
               continue;
          }
          sum += nums[i];
      }
     return sum;
    }
    public static void main(String[] args) {
    
      int[] array1 = {1, 2, 2, 1};
      int[] array2 = {13, 1, 2, 13, 3, 3,13,5,13,2,13,1,1,1};
      System.out.println(sum13(array1));
      System.out.println(sum13(array2));
    }
    

We can create arrays and assingning values like below.

import java.util.*;
class Example{
public static void main(String args[]){
    //--------Method 1-----------------
    int[] a=new int[5];
    int[] b;
    b=new int[5];
    //int[] x=new int[]; //Illegal -->size
    
    //--------Method 2-----------------
    int[] c={10,20,30,40,50};
    int[] d;
    //d={10,20,30,40,50}; //Illegal
            
    //--------Method 3-----------------
    int[] e=new int[]{10,20,30,40,50};
    int[] f;
    f=new int[]{10,20,30,40,50}; //Legal
    
    int[] g=new int[5]{10,20,30,40,50}; //Ilelgal-->size
    }
}

So,In your code(passing array elements as arguments in main methode)should correct like this.System.out.println(sum13(new int[]{1, 2, 2, 1})); System.out.println(sum13(new int[]{13, 1, 2, 13, 3, 3}));

Other thing is nums[i]-- wrong.If we want to get i-1 element we should code as nums[i-1].So, whole code is below.

import java.util.*;
class Demo{
public static int sum13(int[] nums) {
    int sum = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == 13 || ( i > 0 && nums[i-1] == 13)) {
            continue;
        }
    sum += nums[i];
    }
 return sum;
}

public static void main(String[] args) {

    System.out.println(sum13(new int[]{1, 2, 2, 1}));
    System.out.println(sum13(new int[]{13, 1, 2, 13, 3, 3}));
   }

}
Related