0

I have a question on how to call an array static method. In my case, I believe that my array static method is fine because the compiler does not complain about it, but it does when I call it, it says double cannot be converted to double[] How can I fix this? Any help will be greatly appreciated. Below is a snippet of my code:

// create allowed x values for calculation static method
public static double[] allowedValuesX(double[] x, double[] y, int choice){
    double allowedVx[] = new double[choice];
    int i = 0;
    for(i = 0; i < allowedVx.length; i++){
        if((Math.pow(x[i], 2) + (Math.pow(y[i], 2))) <= 1){
            allowedVx[i] = x[i];
        }
    }
    return allowedVx;
}
// main method
public static void main(String args[]){
// create Scanner object
    Scanner in = new Scanner(System.in);

    // call to promptUser
    promptUser();

    // create variable for recieving user input
    int choice = in.nextInt();

    double x[] = new double[choice];
    int i = 0;
    for(i = 0; i < x.length; i++){
        x[i] = Math.random();
    }
// call to allowed x values
    allowedValuesX(x[i], y[j], choice);
1
  • 1
    allowedValuesX(x[i], y[j], choice); vs double[] x, double[] y, int choice. Do you see the difference? Your signature expects an array but you're calling it with a single value from the array (aka: a single double). Commented Dec 13, 2013 at 0:06

1 Answer 1

3

You are passing specific array elements when calling allowedValuesX:

allowedValuesX(x[i], y[j], choice);

But you should pass the arrays themselves, to match the parameter types of the method:

allowedValuesX(x, y, choice);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.