-1

I am just confused about how to store the user's input for the array of numbers. User can input how many numbers they want in the array, but it doesn't ask for the numbers themselves.

import java.util.Scanner;

public class Exercise_0_2 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("How many numbers: ");
int n = input.nextInt();

System.out.println("Enter  " + n + " numbers : ");
double[] numbers = new double[n];

//Call and print methods
System.out.println("Mean: " + mean(numbers, n));
System.out.println("Standard Deviation: " + deviation(numbers, n));

}
// Calculate Mean
public static double mean(double[] numbers, int n) {

    double sum = 0;

    for (int i = 0; i < n; i++) {
        sum += numbers[i];
    }
    double mean = sum / n;
    return mean;
    }
    //Calculate deviation from the mean
    public static double deviation(double[] numbers, int n) {

            double mean = mean(numbers, n);
            double sqSum = 0;
            for (int i = 0; i < n; i++) {
                sqSum += numbers[i] * numbers[i];
            }

                double variance = sqSum / n - mean * mean;
                double sd = Math.sqrt(variance);
                return sd;

    }
}
  • From the help centre: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See:* [mcve] – High Performance Mark Jan 28 '19 at 17:17
  • you not calling mean method – Tony Jan 28 '19 at 17:17
  • Ask for how many numbers -> create array -> ask for numbers and add to array -> call mean with array as parameter -> call deviation with array as parameter. – Joakim Danielson Jan 28 '19 at 18:25
  • ...and not to forget about: `return` statements! (any non-`void` (return type) method MUST return the declared type (in any "logical branch"!(if/else)) ...or throw an Exception;) – xerx593 Jan 28 '19 at 18:38
  • ..but the math looks ok! ...it's really "only syntax" – xerx593 Jan 28 '19 at 18:41

2 Answers2

1

Let’s go over the errors from the top.

    int arr[] = new int[(int) n];

At this point n is not yet declared, so you cannot use it here. Just move the declaration of arr to after declaring and reading n from the scanner. As an aside consider type int for n. double doesn’t really make sense.

    System.out.println("Mean: " + mean);

If this was supposed to be a call to the method mean, you need to put brackets after the method name and the required arguments in the brackets, as in mean(arr). mean(arr) still won’t work because arr has type int[] while the method requires type double[]. I cannot tell you what the right fix is here, since I am not sure whether you meant to read the numbers outside or inside the method. Outside would seem more natural to me.

    System.out.println("Enter" + n + " numbers: ");

You cannot use the variable n here since it is a local variable in the main method. Then again I am not sure you need it if you intended to read the numbers elsewhere and pass a filled array into the method.

        array[i] = input.nextDouble();

Two errors here: You don’t have an array variable named array1. You may have meant numbers, I am not sure. And you cannot use the variable input here since it is a local variable in the main method (same problem as with n above). Either declare it a static field instead or pass it as an argument to the method.

The errors in the deviation method are similar, so I will not repeat.

Since I understand that this should be an exercise for you, I am not going to solve it for you and deprive you of the learning. I hope you’re getting a step or two further.

Edit

There are some fine improvements in the edit of your question. You still don’t seem to have decided whether to read the numbers in main or in mean?

    System.out.println("Mean: " + mean(numbers, n));

There isn’t a variable numbers declared in your main method (same problem in the following line). Otherwise fine.

        sum += a[i];

You probably meant meanSum and numbers? Or maybe you meant to declare sum instead of meanSum above the for loop?

There’s a right curly brace missing after above line to end the first for loop.

    double dqDiff = 0;

Simple typo: you are referring to this variable as sqDiff later, so need to use the same spelling here in the declaration.

    return Math.sqrt(mean(number, n));

Missing s on numbers. I am not convinced that this simple version of the math is correct.

Please don’t expect me to take any more rounds on this code.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

I can recommend to first use an ArrayList instead of an array[], in this case you have no to indicate quantity of numbers (n) and can freely enter so much numbers as you'd like. Second, use a try-catch block to stop entering numbers and initialization of calculations. For example:

public class MeanAndDeviation {

public static void main(String[] args) {

    Scanner scn = new Scanner(System.in);

    System.out.println("Enter the numbers then press any letter to calculate Mean and Sd");
    /*Use an ArrayList to freely enter so much numbers as you need*/
    ArrayList <Double> arr = new ArrayList <>();
    /*Use try-catch block to start calculations by entering any letter*/
    try {
        while (scn.hasNext()) {
            double i = scn.nextDouble();
            arr.add(i);
        }
    }
    /*Use catch (Exception e) to catch any type of exceptions*/
    catch (Exception e) {}

    System.out.println(arr + "\nMean: " + mean(arr) + 
            "\nStandard Deviation: " + deviation(arr));
    }

private static double mean(ArrayList<Double> arr) {
    double sum = 0;
    for (int i = 0; i < arr.size(); i++) {
        sum += arr.get(i);}
    double mean = sum/arr.size();
    return mean;       
}

private static double deviation (ArrayList<Double> arr) {
    double sumOfDiff = 0;
    for (int i = 0; i < arr.size(); i++) {
        sumOfDiff += Math.pow(arr.get(i) - mean(arr), 2);
    }
    double deviation = Math.sqrt(sumOfDiff/arr.size());
    return deviation;
}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sergei Voychuk
  • 171
  • 2
  • 8