-3

Can someone please explain to me what I am doing wrong on the code below? I will appreciate it! I am getting the error "incompatible types: double cannot be converted into double[].

public class AverageFunction {
    public static double mean(double[] a) { 
        double sum = 0.0;
        for (int i = 0; i < a.length; i++)
            sum += a[i];
        return sum / a.length;
    }
    public static void main(String[] args) {
    double[] a = StdIn.readDouble();
    double result = mean(a);
    StdOut.println(result);
    }
}
    
C.Sanchez
  • 25
  • 1
  • 7
  • 3
    First you should tell us how you know that something is wrong: Does the code not compile? Does it produce an Error when running it? Does it produce wrong results? – OH GOD SPIDERS Jul 06 '20 at 14:55
  • 1
    What is wrong with the code? Compiler error? If so: please include the error. Unexpected behaviour? If so: please include expected and observed behaviour. An exception? If so: please include the stack trace. – Turing85 Jul 06 '20 at 14:55
  • Can you also tell what is the error while executing the above code? – dhruv tailor Jul 06 '20 at 14:57
  • You never define what `StdIn` or `StdOut` is. And the rest of the line `double[] a = StdIn.readDouble();` looks troubling as well. – f1sh Jul 06 '20 at 14:57
  • Check https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java – Arvind Kumar Avinash Jul 06 '20 at 14:58
  • 4
    `double[] a = StdIn.readDouble();` you are reading a single double but you try to store it as an array of double – jhamon Jul 06 '20 at 14:58
  • Dear jhamon, Thank you very much for your time and for answering the question. I do appreciate it! – C.Sanchez Jul 06 '20 at 15:13
  • @jhamon you should write that as an answer if this gets re-opened and earn the reputation for being helpful – Sascha Jul 07 '20 at 14:02

1 Answers1

0

The readDouble() method only read a single double.

Your are trying to assign the returned value to an array of double. The types mismatch.

To read multiple double, you can use the readAllDoubles() method. Check the javadoc (I can't find any other source for this library) for more informations.

jhamon
  • 3,346
  • 3
  • 23
  • 35