title: Array index out of bounds error even though the indices are within the array length
body: <p>I was trying to write a java program where every digit of an input integer is printed in words.</p>
  <p>For example: <em>input</em> <code>123</code> should produce an <em>output</em> <code>&quot;one two three&quot;</code>.</p>
  <p>I wrote the following program that takes an integer value, then converts it into a string. I then iterated over the characters of the string and converted them to integer values, which I later used as indices for the array.</p>
  <p>But I'm getting <code>ArrayIndexOutOfBoundsException</code>.</p>
  <pre><code>Index 49 out of bounds for length 10
  </code></pre>
  <p><em>My code:</em></p>
  <pre><code>public class DigitsAsWords {
      static void Print_Digits(int N){
          String arr[] = {&quot;zero&quot;,&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;,&quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;};
          String st = Integer.toString(N);
          System.out.println(st);
          char s;
          int a;
          for (int i=0; i&lt;st.length(); i++){
              s = st.charAt(i);
              a = Integer.valueOf(s);
              System.out.print(arr[a]+&quot; &quot;);
          }
      }
      public static void main (String args[]){
          Scanner sc = new Scanner(System.in);
          int a = sc.nextInt();
          Print_Digits(a);
      }
  }
  </code></pre>