title: Java String Index out of Range, dont know whats wrong
body: <p>Can anyone see why or where my code would be giving me trouble, i am getting a string index out of range : -1 error.</p>
  
  <p>What i am trying to do is get input from the user via GUI and take that input and check to see if it contains any digits i.e. 1,2,3... and convert them to Strings ("one", "two" , "three"),
  by replacing them in a sentence. </p>
  
  <p>so this sentence ( I have 9 cats and 1 dog)
  would result in  ( I have nine cats and one dog )</p>
  
  <p>im not sure what im doing wrong or if what im tempting to do is legal programming, I'm very new to it as you can tell. </p>
  
  <p>thanks guys!!</p>
  
  <pre><code>private String getResultStr()
  {
      /*** Local Variables ***/
  
      StringBuffer result;
      String userInput;
      int digit;
      int count;
      String finalResult = "";
          String digitAsStr;
  
      /*** Instantiate ***/
  
      result = new StringBuffer();
  
      /*** Extract Digits (if any) ***/
  
      userInput = txtInput.getText();
      result.append( userInput );
  
      count = 0;
  
      while ( count &lt; result.length() )
      {
  
          for ( int i = 0; i &lt; result.length(); i++ )
              if ( result.charAt( i ) &gt;= '0' || result.charAt( i ) &lt;= '9' )
              {
                  digit = result.charAt( i );
                  digitAsStr = digitToString( digit );
                  result.deleteCharAt( i );
                  result.insert( i - 1, digitAsStr );
              }
  
          count++;
      }
  
      finalResult = result.toString();
  
      return finalResult;
  
  }
  </code></pre>