title: Java charAt() String index out of range
body: <p>I am trying to figure out "what 5-digit number when multiplied by 4 gives you its reverse?" using this code but I get error: </p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
  String index out of range: 5 at java.lang.String.charAt(String.java:658) at     
  Digits.main(Digits.java:15)
  </code></pre>
  
  <p>I would like to figure out (have someone explain) why this is happening. I would like to keep my charAt in my code and not use <code>StringBuilder (StringBuilder.reverse())</code> if that is possible.</p>
  
  <pre><code>public class Digits{
    public static void main(String[] args) {
      int n = 0;
      int b = 0;
      String number = "";
      String backwards = "";
  
      for (int x = 9999; x &lt;= 99999 ; x++ ) {
        n = x;
        b = x * 4;
        number = Integer.toString(n);
        backwards = Integer.toString(b);
  
        if ( number.charAt(0) == backwards.charAt(4) &amp;&amp; number.charAt(1) == backwards.charAt(3)
        &amp;&amp; number.charAt(2) == backwards.charAt(2) &amp;&amp; number.charAt(3) == backwards.charAt(1)
        &amp;&amp; number.charAt(4) == backwards.charAt(0)) {
          System.out.println(n);
          break;
        }
      }
  </code></pre>
  
  <p>Thanks</p>