title: java.lang.ArrayIndexOutOfBoundsException: 3 error
body: <p>I have the following piece of code for which i am getting <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html" rel="nofollow"><code>java.lang.ArrayIndexOutOfBoundsException</code></a></p>
  
  <pre><code>//divides ACode by 10 as many times as specified by the DecimalDigitPosition 
  public static int getDecimalDigit(int ACode, int decimalDigitPosition) {
      if (decimalDigitPosition == 0)
          return ACode;
      else {
          ACode = ACode / (10 * decimalDigitPosition);
          int remainder = ACode % 10;
          return remainder;
      }
  }
  </code></pre>
  
  <p>I am suspecting this line...</p>
  
  <pre><code>digit[i] = getDecimalDigit(samples[i].getValue(), 0);
  </code></pre>
  
  <p>in the code below which could be the reason for this exception.</p>
  
  <pre><code>//start a method to decode the message.
  public static void decodeMessage(String filename) {
      Sound s = new Sound(filename);
      SoundSample[] samples = s.getSamples();
      int[] digit = new int[3];
      String message = "";
      int sampleIndex = 0;
      boolean nullReached = false;
      while (!nullReached) {
          int asciiValue = 0;
          for (int i = sampleIndex; i &lt; sampleIndex + 3; i++) {
              if (i &lt; samples.length) {
                  // this line could be the cause of error..
                  digit[i] = getDecimalDigit(samples[i].getValue(), 0);
                  asciiValue += digit[i - sampleIndex] * (((i - sampleIndex) == 0) 
                  ? 1 
                  : ((i - sampleIndex) * 10));
              } else {
                  for (int j = 0; j &lt; 3; j++) {
                      digit[j] = 0;
                  }
                  break;
              }
          }
          if (digit[0] == digit[1] 
          &amp;&amp; digit[1] == digit[2] 
          &amp;&amp; digit[2] == 0) 
          nullReached = true;
          message += (char) asciiValue;
      }
  }
  </code></pre>
  
  <h3>Stack trace:</h3>
  
  <blockquote>
    <p>java.lang.ArrayIndexOutOfBoundsException: 3 at
    project.decodeMessage(project.java:107) at
    project.main(project.java:33) at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav‌​a:25)
    at java.lang.reflect.Method.invoke(Method.java:597) at
    edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​2)
    ></p>
  </blockquote>