0

I am trying to configure an app that calculates the product of a series of doubles that are passed to the method calcProduct. calcProduct will have a variable length arguements and should return the product that is calculated. This is supposed to then be printed with the array's identification.

Here is my code so far:

import java.text.DecimalFormat; import java.text.NumberFormat;

public class variableLength {

public static double calProduct(double... numbers)
{
    double total = 0.0;

    for (double number : numbers)
        total += number;

    return total / numbers.length;
}

public static void main(String[] args)
{

    NumberFormat myFormat = NumberFormat.getInstance();
    myFormat.setGroupingUsed(true);

    DecimalFormat decimalFormat = new DecimalFormat("#.###");
    decimalFormat.setGroupingUsed(true);
    decimalFormat.setGroupingSize(3);


    double[] array1 = {2.1, 3.2, 4.3};
    double[] array2 = {1, 2, 3, 4, 5};
    double[] array3 = {2.0, 110.12345};
    double[] array4 = {10.0, 12, 13.1, 4, 5.2, 6, 7.3, 8, 9.4, 100};
    double[] array5 = { };

    System.out.println(myFormat.format(array1));
    System.out.println(myFormat.format(array2));
    System.out.println(myFormat.format(array3));
    System.out.println(myFormat.format(array4));
    System.out.println(myFormat.format(array5));

    System.out.println(decimalFormat.format(array1));
    System.out.println(decimalFormat.format(array2));
    System.out.println(decimalFormat.format(array3));
    System.out.println(decimalFormat.format(array4));
    System.out.println(decimalFormat.format(array5));


   System.out.printf("array1 = %.1f\narray2 = %.1f\narray3 = %.1f\narray4 = %.1f\narray5 = %.1f\n\n", array1, array2, array3, array4);

    System.out.println("The average of array1 and array2 is \n" + calProduct(array1, array2));
    System.out.println("The average of array1, array2, and array3 is \n" + calProduct(array1, array2, array3));
    System.out.println("The average of array1, array2, and array3, array4 is \n" + calProduct(array1, array2, array3, array4));
    System.out.println("The average of array1, array2, array3, array4 and array5 is \n" + calProduct(array1, array2, array3, array4, array5));
}

}

I keep getting this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[]) The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[], double[]) The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[], double[], double[]) The method calProduct(double...) in the type variableLength is not applicable for the arguments (double[], double[], double[], double[], double[])

at variableLength.main(variableLength.java:52)

What am I doing wrong? How can I achieve a working program? Thank you in advance!

  • Does this answer your question? [How can I concatenate two arrays in Java?](https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – Ward Dec 08 '19 at 18:44

2 Answers2

0

Your error is saying that what you are giving to the function calProduct is not what you declared in the function.

public static double calProduct(double[] numbers)

This way you tell java you will give an array which contains doubles.

Also, it says that you only specified one parameter. This means that java doesn't expect you to hand in multiple arrays.

public static double calProduct(double[] numbers, double[] number 2)

I hope you get the idea. You can also include an array in another array, then you only have to pass one array.

Functions in java Array in java

0

The issue is that your calProduct function is written to take a series of doubles, but when you actually called the function, your pass it a series of arrays of doubles. Since those types are not compatible, you get the compilation error you described.

If you wanted to edit the calProduct function to take a series of arrays of doubles, you would also want to use two for loops, one to loop through the series of arrays, and one to loop through the numbers in each array. You would also need to track the count of the how many doubles you have, since it looks like you are actually calculating an average, not just the product.

For example, the rewritten calProduct function might looks like this:

public static double calProduct(double[]... numberArrays) {
    double total = 0.0;
    int count = 0;

    for (double[] numbers : numberArrays) {
        for (double number : numbers) {
            total += number;
            count++;
        }
    }

    return total / count;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Andrew Merrill
  • 1,672
  • 11
  • 14