1

I am a newbie here and I try to write a program that will be calculating the deviation of 10 numbers in the array, here is my code that I got created :

package week10;

import java.util.Scanner;

public class deviation {
    public static void main(String[]args) {

        Scanner input = new Scanner(System.in);
        double testScores[] = new double [10];
        double sum = 0;

        int count = 0;
        int count2 = 0;
        int count3 = 0;

        double inputDouble = 0;
        int arrayIndex = 0;

        //GET INPUT TEST SCORES
        while(inputDouble >= 0) {
            System.out.print("Enter Score: ");

            inputDouble = input.nextDouble();
            if(inputDouble >= 0)
            {
                testScores[arrayIndex] = inputDouble;
                sum += inputDouble;
            }
            arrayIndex++;
        }

        if(arrayIndex < testScores.length)
        {
            for (int x = arrayIndex-1; x <= testScores.length-1; x++)
            {
                testScores[x] = -1;
            }
        }

        //GET NEW ARRAY WITH ONLY VALID SCORES
        double[] validScores = GetValidScores(testScores, arrayIndex-1);

        System.out.println(" The mean is: " + mean(validScores));
        System.out.println(" The standard deviation is: " + deviation(validScores));            
    }

    private static double[] GetValidScores(double[] inputArray, int validScoreCount) {
        double[] newScores = new double[validScoreCount];
        for(int z = 0; z < validScoreCount; z++)
        {
            newScores[z] = inputArray[z];
        }
        return newScores;
    }

    public static double deviation(double[] values) {
        double sum = 0.00;

        double theMean = mean(values);

        for(int i =0; i < values.length; i++) {
            double currentCalc = values[i] - theMean;
            sum += Math.pow(currentCalc, 2);
        }

        sum = sum / (values.length -1);

        sum = Math.sqrt(sum);

        return sum;
    }

    public static double mean(double[] values)
    {
        double sum = 0.00;

        for(int i=0; i < values.length; i++)
        {
            sum += values[i];
        }

        return sum / values.length;
    }

}

Output:

Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at week10.deviation.main(deviation.java:26)"

I understand that array is 10, but it starts with a 0, so this is why I chose command array-1, can you please tell me or show what am I doing wrong?

2
  • 1
    Actually arrayIndex starts at 0 and goes up to 9, so when you exit the while loop, if you only enter 10 numbers, your arrayIndex will be 9. So you're actually passing 8 when you give it arrayIndex - 1. Commented Apr 12, 2014 at 19:04
  • Try debugging manually at The line 26 Commented Apr 12, 2014 at 19:06

2 Answers 2

2

You have an array of 10 elements, and ask for the next element, in a loop, until the user enters a negative value. Since the user always enters 25, the loop never stops, until it reaches the 11th element. Accessing the 11th element (at index 10) of an array of 10 elementts throws the exception you're seeing.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to read exactly 10 numbers, change reading input part like this

//GET INPUT TEST SCORES
for(int i=0;i<10;i++)    
    System.out.print("Enter Score: ");

    inputDouble = input.nextDouble();
    testScores[i] = inputDouble;
    sum += inputDouble;
}

1 Comment

I see, now, basically with the idea of the array length 10, and position of the 10th number being 9 in the array, I misinterpreted the logic how it worked, and that I can end it before 11th input with a negative number.

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.