Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default 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);
    double[] proportionTestBenfordArray = calculatingLeadingDigitProportions(testBenfordArra y);
    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);
    double[] proportionTestRandomNumbersArray = calculatingLeadingDigitProportions(testRandomNumbe rsArray);
    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];


    //Adding values to the array
    for(int i=1;i<numberPeriods;i++){



    return arrayBenford;


    public static double[] calculatingLeadingDigitProportions(double[] numbers){

    double[] calculatedArray;

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

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

    if((calculateLeadingDigit((int)numbers[i]))==0.0) calculatedArray[0]++;
    if((calculateLeadingDigit((int)numbers[i]))==1.0) calculatedArray[1]++;
    if((calculateLeadingDigit((int)numbers[i]))==2.0) calculatedArray[2]++;
    if((calculateLeadingDigit((int)numbers[i]))==3.0) calculatedArray[3]++;
    if((calculateLeadingDigit((int)numbers[i]))==4.0) calculatedArray[4]++;
    if((calculateLeadingDigit((int)numbers[i]))==5.0) calculatedArray[5]++;
    if((calculateLeadingDigit((int)numbers[i]))==6.0) calculatedArray[6]++;
    if((calculateLeadingDigit((int)numbers[i]))==7.0) calculatedArray[7]++;
    if((calculateLeadingDigit((int)numbers[i]))==8.0) calculatedArray[8]++;
    if((calculateLeadingDigit((int)numbers[i]))==9.0) calculatedArray[9]++;


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

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


    return calculatedArray;


    public static int calculateLeadingDigit(int number){

    if (number == 0)
    return 0;
    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.

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    My Mood
    Thanked 263 Times in 232 Posts

    Default 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.

Similar Threads

  1. Comments or Suggestions on Some Code
    By mike_p in forum Object Oriented Programming
    Replies: 2
    Last Post: March 8th, 2012, 05:51 PM
    By igkarthi in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2012, 07:52 AM
  3. Error for a simple array
    By DudeJericho in forum Collections and Generics
    Replies: 4
    Last Post: April 25th, 2011, 02:46 PM
  4. Illegal Start of Expression Error. Any help is appreciated.
    By SKhoujinian91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 8th, 2009, 12:57 AM

Tags for this Thread