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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

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
    Posts
    7
    Thanks
    0
    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.

    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(testBenfordArray);
        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(testRandomNumbersArray);
        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;
     
        //Adding values to the array
        for(int i=1;i<numberPeriods;i++){
     
          arrayBenford[i]=arrayBenford[i-1]*(1+growthRate);
     
        }
     
        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;
        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;
      }
    }
     
     
     
     
     
     
     
     
     
     
    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;
        }
    }
    public static void main(String[] args)
    {
    }



    When i run this it runs a few things and then i get 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)
    >

    i realize it is telling me i have an issue with my array1 and array2 values but because in the assignment it notes to just assume that the array lengths are the same i figure the code should work. Any comments or way to resolve this issue would be greatly appreciated.
    Thanks


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: It seems i have an error with my array but i'm not sure why any comments apprecia

    for starters check your for loop inside generateBenfordNumbers... does that code compute for every element in the array or does it stop one short?

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: It seems i have an error with my array but i'm not sure why any comments apprecia

    Quote Originally Posted by JoshKesner View Post
    ...
    i realize it is telling me i have an issue with my array1 and array2 values ...
    What the heck is the calculateDistance supposed to be calculating? (Answer: The distance between two arrays of equal length.)

    For what two arrays are you supposed to be calculating the distance?

    Aren't you supposed to do something like the following:

    • Call your generateBenfordNumbers method to create an array of 1000 points and then call your calculateProportions method to obtain an array of proportions. (Ten elements, right?)

    • Calculate the distance between this array of proportions and the ten-element array returned by the BenfordSupportCode.getBenfordProbabilities method.

    • Call the generateRandomNumbers method from the BenfordSupportCode class to create an array of 1000 points and call your calculateProportions method to obtain another array of proportions. (Ten elements, right?)

    • Calculate the distance between this array of proportions and the ten-element array returned by the BenfordSuppordCode.getBenfordProbabilities method.

    Or what?

    I mean, that's what you have shown for which it makes sense to be calculating any kind of distance. Or so it seems to me.


    Cheers!

    Z
    Last edited by Zaphod_b; October 23rd, 2012 at 04:03 PM.

Similar Threads

  1. Replies: 1
    Last Post: October 22nd, 2012, 07:19 PM
  2. Comments or Suggestions on Some Code
    By mike_p in forum Object Oriented Programming
    Replies: 2
    Last Post: March 8th, 2012, 05:51 PM
  3. Comments
    By igkarthi in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2012, 07:52 AM
  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