-1

I cannot figure out what is the error with my input argument, the compiling errors messages are not helping much.

 import java.util.ArrayList;

/**
 * Created by cheun on 11/8/2017.
 */
public class Problem2_ProductsOfAllIntsExceptAtIndex {
    static int pointer;
    static int[] arr;
    static int[] temp_arr;
    static int input;

    public static int[] myFunction(int[] arg) {
        // write the body of your function here
        for (int i = 0; i <= arg.length; i++) {
            pointer = i;
            temp_arr = arg;
            temp_arr[i] = 1; // or should put it null
            for (int j = 0; i <= arr.length; i++) {
                input *= temp_arr[j];
            }
            arr[pointer] = input;
        }
        return arr;

    }

    public static void main(String[] args) {
        Problem2_ProductsOfAllIntsExceptAtIndex solution = new Problem2_ProductsOfAllIntsExceptAtIndex();
// line 32 FAULTY LINE BELOW
            System.out.println(myFunction([0, 1, 2, 3]));
// line 32 FAULTY LINE ABOVE    
        }

    }

D:\java_workspace\InterviewQuestions\com.cake.interviews\src\Problem2_ProductsOfAllIntsExceptAtIndex.java Error:(32, 39) java: illegal start of expression Error:(32, 40) java: ')' expected Error:(32, 41) java: ';' expected Error:(32, 43) java: not a statement Error:(32, 44) java: ';' expected

dcas
  • 111
  • 1
  • 10
  • 5
    change `[0, 1, 2, 3]` to `new int[]{0, 1, 2, 3}` – Eran Nov 08 '17 at 06:45
  • If you need more clarification and different ways to create an array in java, you may go through the following link: https://www.javatpoint.com/array-in-java – akshaya pandey Nov 08 '17 at 06:52

2 Answers2

1

Couple of things are wrong in your program. I recommend strongly to learn the basics of the Java language. There are a lot of free resources on the Internet. Here is the Java Tutorials website where you could start. And here you can go directly to arrays in Java.

Having said that the following might be what you're trying to achieve:

public class Problem2_ProductsOfAllIntsExceptAtIndex {

    public static int[] myFunction(int[] arg) {
        // making all variables global (=> static) doesn't make sense in this case
        int input = 1;    
        int[] temp_arr;
        int[] arr ;

        arr = new int[arg.length];
        for (int i = 0; i < arg.length; i++) {
            temp_arr = Arrays.copyOf(arg, arg.length);  // clone the original array; otherwise you overwrite the original one
            temp_arr[i] = 1; // or should put it null
            for (int j = 0; j < temp_arr.length; j++) {
                input *= temp_arr[j];
            }
            arr[i] = input;
            input = 1; // reset
        }
        return arr;

    }

    public static void main(String[] args) {
        int [] arr = myFunction(new int[] { 0, 1, 2, 3 });
        System.out.println(Arrays.toString(arr));
    }

}
ujulu
  • 3,289
  • 2
  • 11
  • 14
0

1.arr is not instantiated-> Null pointer Exception

2.If you want to input an array with numbers use this:

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

or you can change definition of my Function to myFunction(int... arg) and give parameters separated by coma

Example:

public class Problem2_ProductsOfAllIntsExceptAtIndex {
    static int pointer;
    static int[] arr= new int[4];
    static int[] temp_arr;
    static int input;

    public static int[] myFunction(int[] arg) {
        // write the body of your function here
        for (int i = 0; i <= arg.length; i++) {
            pointer = i;
            temp_arr = arg;
            temp_arr[i] = 1; // or should put it null
            for (int j = 0; i <= arr.length; i++) {
                input *= temp_arr[j];
            }
            arr[pointer] = input;
        }
        return arr;

    }

    public static void main(String[] args) {
        Problem2_ProductsOfAllIntsExceptAtIndex solution = new Problem2_ProductsOfAllIntsExceptAtIndex();
// line 32 FAULTY LINE BELOW
            System.out.println(Arrays.toString(myFunction(new int[]{0, 1, 2, 3})));
// line 32 FAULTY LINE ABOVE    
        }

    }
xyz
  • 812
  • 9
  • 18