Dear all,
I am trying to write a code that would take in an array of numbers(salaries) and then calculating the mean and standard deviance. The number of salaries is first asked from the user, therefore the number of elements in the array is not predefined. I have written the code, but it does not give me the right answer, please help me troubleshoot this problem

import java.util.Scanner;

public class Q4_Arrays{
    public static void main(String [] a){

        Scanner input= new Scanner (;
        System.out.println("Please enter the number of employees");
        int noEmp = input.nextInt();
        double [] salaries = new double [noEmp];
        double Var=0;
        double total= 0;
        double sDev;

        for (int i= 1; i<=salaries.length; i++){
            System.out.println("Please enter Salary");
            double Salary=input.nextDouble();
            total= total + Salary ; }

        double average = total/noEmp;
        System.out.println("The average salary of your company is "+ average);

        for (int i=1; i<salaries.length;i++)
            Var = Var + (Math.pow(salaries[i] - average, 2)/salaries.length);

           sDev= Math.sqrt(Var);
        System.out.println("The standard deviation for this company's salaries is " + sDev);


so analyzing the code, I have found that the problem lies within the variance algorithm, it does not give the correct answer

Line 25 you divide the running total of "Var" by the number of salaries on every pass of the loop. I think that's wtrong - you should compute the sum then divide by the number of salaries just once at the end.

I tried this, didn't work, suggestions?

for (int i=0; i<salaries.length;i++)
            Var = Var + (Math.pow(salaries[i] - average, 2));          

        Sfinal= Var/salaries.length;

           sDev= Math.sqrt(Sfinal);
        System.out.println("The standard deviation for this company's salaries is " + sDev);

"Didn't work" - you mean the program wouldn't execute? Please be very specific about what errors you are getting (eg complete error messages or actual vs expected results).

That latest code looks right now. I suspect you are getting zero as the result? Maybe becuase you never actually store any data in the array?

Try using Sfinal = Var/(salaries.length - 1); for an explanation see here Yes, stats is not often everones favorite. ;-)

sorry for that
basically, the code executes perfectly fine without any errors, but the answer, the Standard Deviation it calculates, is wrong.

That's still not enough information. If the result is wrong: (1) what is it? (2) what should it be?
ps: see my previous post - did you store the data into the array?

ok, well, the code when executed, asks the user to enter the number of employees, say 5, then it asks you to enter their salaries, say 10, 12,14,16,18. Now, the average is 14, it prints out correct. But then, the standard deviation, it should be approx. 2 but my code shows it to be, 15.65

see my previous posts - did you store the data into the array?

yes, all inputs are stored in the array, if that is not what you are asking, I am understanding what you mean, sorry.

In the code you posted above the inputs are NOT stored in the array.
The array salaries is created on line 9 and its values are used on line 25, but there is no code that puts the values into it.

No you have not stored the data into the array. You declared an array salaries at line 9 but you have not used this anywhere until you are trying to access the data (which there are none in the array) in the second for loop.

Hint: Carefully look at the first for loop

Hope that helps :)

@kal crazy, thanks buddy. I have managed to compile the code perfectly now.