# It seems i have an error with my array but i'm not sure why any comments appreciated.

• October 22nd, 2012, 07:07 PM
JoshKesner
It seems i have an error with my array but i'm not sure why any comments appreciated.
ok here is my code in complete:

public class BenfordsLaw{

public static void main(String[] args){

System.out.print("Generating Benford Sequence with initial amount =" + 100);
System.out.print(", growth rate =" + .1);
System.out.println(", number of periods =" + 1000);
double[] testBenfordArray = generateBenfordNumbers(100,.1,1000);
for (int i = 0; i<10; i++) System.out.println("In calculating Leading Digit Proportions, digit " + i + " appears " + proportionTestBenfordArray[i] + " proportion of the time.");

double[] testRandomNumbersArray = BenfordSupportCode.generateRandomNumbers(1000);
for (int i = 0; i<10; i++) System.out.println("In calculating Random Leading Digit Proportions, digit " + i + " appears " + proportionTestRandomNumbersArray[i] + " proportion of the time.");

double idealDistribution = calculateDistance(testBenfordArray, BenfordSupportCode.getBenfordProbabilities());
System.out.println("The ideal Benford distribution is" + idealDistribution);
double randomDistribution = calculateDistance(testRandomNumbersArray, BenfordSupportCode.getBenfordProbabilities());
System.out.println("The random distribution is" + randomDistribution);

}

public static double[] generateBenfordNumbers(double initialAmount, double growthRate, int numberPeriods){

double[] arrayBenford;

//creates number of spots in the array equal to value of numberPeriods
arrayBenford = new double[numberPeriods];

arrayBenford[0]=initialAmount;

for(int i=1;i<numberPeriods;i++){

arrayBenford[i]=arrayBenford[i-1]*(1+growthRate);

}

return arrayBenford;

}

double[] calculatedArray;

//Creates 10 spots in the array
calculatedArray = new double[10];

for(int i=0;i<numbers.length;i++){

}

for(int i=0;i<calculatedArray.length;i++){

calculatedArray[i] = (calculatedArray[i]/numbers.length);

}

return calculatedArray;

}

if (number == 0)
return 0;
else{
double bigNumber = Math.abs(number);
while (bigNumber >= 10.0)
bigNumber = bigNumber/10.0;
int intBigNumber = (int) bigNumber; //cast bigNumber to be an int for the return statement

return intBigNumber;
}
}

public static double calculateDistance(double[] array1, double[] array2){
double euclideanDistance = 0.0;
for(int i=0;i<array1.length;i++){
euclideanDistance = euclideanDistance + Math.pow((array1[i]-array2[i]), 2);
}
euclideanDistance = Math.sqrt(euclideanDistance);
return euclideanDistance;
}
}

and another method is:

import java.util.Random;

public class BenfordSupportCode
{
//this method returns an array of random integers
public static double[] generateRandomNumbers(int count)
{
//Note FYI: if you want to get a different sequence each time, change the number 1
//below to something else. However, it will be easier to debug any issues you have
//if you leave this consistant as you can then reproduce your errors consistantly.
Random randGenerator = new Random(1);

double[] random = new double[count];
for (int i = 0; i < count; i++)
{
random[i] = randGenerator.nextInt(10000);
}

return random;
}

//this method returns an array of the benford probabilities
public static double[] getBenfordProbabilities()
{
double[] benfordDistribution = {0, .301, .176, .125, .097, .079, .067, .058, .051, .046};
return benfordDistribution;
}
}

When i attempt to run my code it runs a few things and then it hits the error: java.lang.ArrayIndexOutOfBoundsException: 10
at BenfordsLaw.calculateDistance(BenfordsLaw.java:100 )
at BenfordsLaw.main(BenfordsLaw.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
>

While I realize this is probably because i have not declared the array index for array2 i don't think it should matter, because for my assignment the specifications are that you can assume that array1 and array2 are of the same length.
Any help and or comments would be greatly appreciated.
Thanks
• October 22nd, 2012, 07:19 PM
jps
Re: It seems i have an error with my array but i'm not sure why any comments apprecia
Please use code tags on the code in your post. See the announcements page for help on using code tags. If you delete the code and paste again using code tags, it will correct the indentation also.