-3

Here's my code:

public class JavaApplication16 {
    public static void main(String[] args) {
       //illegal start of expression, ']' expected, <identifier> expected
       boolean a = groupSum(0, [2,4,8], 10); 
    }

    public boolean groupSum(int start, int[] nums, int target) {
        if (start >= nums.length) return (target == 0);

        if (groupSum(start + 1, nums, target - nums[start])) return true;
        if (groupSum(start + 1, nums, target)) return true;

        return false;
    }
}

I've tried to look for ways to fix this, but I am just so lost. Please help me debug my code.

Ahmad Al-Kurdi
  • 1,887
  • 2
  • 16
  • 35
Josh
  • 5
  • 2
  • 2
    try `boolean a = groupSum(0, new int[]{2,4,8}, 10);` and make the method `static`. – Ousmane D. Dec 24 '17 at 21:39
  • 2
    You get this error because this is not the correct syntax to define an array. – Turing85 Dec 24 '17 at 21:39
  • Here is answer for your next question: [Non-static method/variable cannot be referenced from a static context](https://stackoverflow.com/q/2559527) – Pshemo Dec 24 '17 at 21:40

4 Answers4

3

You are not using the correct notation to create an array. Creating an array of ints looks like: new int[]{1, 2, 3}. Another problem present in your code, is that you are calling a non-static method from a static context. You can fix this by labeling groupSum as static. Here is fixed version of your code:

class JavaApplication16 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //illegal start of expression, ']' expected, <identifier> expected
        boolean a = groupSum(0, new int[]{2,4,8}, 10);
    }
    public static boolean groupSum(int start, int[] nums, int target) {
        if (start >= nums.length) return (target == 0);

        if (groupSum(start + 1, nums, target - nums[start])) return true;
        if (groupSum(start + 1, nums, target)) return true;

        return false;
    }
}
Infuzion
  • 572
  • 5
  • 16
0

Here's the correct way to work with int arrays.

boolean a = groupSum(0, new int[] {2, 4, 8}, 10);
0

You need to change your code to be as the following

1- new int[]{2,4,8} to create array arrays of ints

2- change groupSum method to be static or create object form JavaApplication16 as the following JavaApplication16 ja16= new JavaApplication16(); ja16.groupSum(...);

You can do the following to fix your issues.

 public class JavaApplication16 {
            public static void main(String[] args) {
            boolean a = groupSum(0, new int[]{2,4,8}, 10);// new int[]{2,4,8} to create array of ints 
        }

        public static boolean groupSum(int start, int[] nums, int target) {// change the method to be static
            if (start >= nums.length) return (target == 0);

            if (groupSum(start + 1, nums, target - nums[start])) return true;
            if (groupSum(start + 1, nums, target)) return true;

            return false;
        }

}
Ahmad Al-Kurdi
  • 1,887
  • 2
  • 16
  • 35
0

you have to define the int array as

boolean a = groupSum(0, new int[] {2,4,8}, 10);

another thing, The method groupSum is an instance method, while main is a static method; you cant use instance method inside a static mehod.

You have two solutions for this:

  1. Define the groupSum method as static.
  2. Create object of your class JavaApplication16 and use its method. The code would be like:

    public class JavaApplication16 { 
     public static void main(String[] args) {
       JavaApplication16 ja16 = new JavaApplication16();
       boolean a = ja16.groupSum(0, new int[] {2,4,8}, 10); 
     }
    
     public boolean groupSum(int start, int[] nums, int target) { 
      if (start >= nums.length) return (target == 0); 
      if (groupSum(start + 1, nums, target - nums[start])) return true; 
      if (groupSum(start + 1, nums, target)) return true;
    
      return false; 
     } 
    }