title: How do I add characters to a string immediately before the occurrence of all numbers between the first and last ones in Java?
body: <p><strong>SCENARIO</strong></p>
  <p>I have a program in Java that randomly puts out text as follows:</p>
  <p><em>1 egg1 chicken and 2 ducks</em></p>
  <p><em>2 cows1 pig1 egg and 2 cats</em></p>
  <p><em>1 hour1 dog1 ostrich 2 cows and 3 chickens</em></p>
  <p><strong>QUESTION:</strong></p>
  <p><strong>How do I add a comma and a space within a string immediately before the occurrence of all numbers between the first and last ones?</strong> That is, how can I format these strings so that these cases look as follows:</p>
  <p><em>1 egg, 1 chicken and 2 ducks</em></p>
  <p><em>2 cows, 1 pig, 1 egg and 2 cats</em></p>
  <p><em>1 tractor, 1 dog, 1 ostrich, 2 cows and 3 chickens</em></p>
  <p>As you will see, in each case I:</p>
  <p><strong>WHAT I HAVE SO FAR:</strong></p>
  <p>I already have it successfully handles cases of:</p>
  <p>1 object:</p>
  <p><em>1 cat</em></p>
  <p>2 objects:</p>
  <p><em>2 cats</em></p>
  <p>Variants of Multiple Objects:</p>
  <p><em>3 cats and 1 ostrich</em></p>
  <p>and</p>
  <p><em>1 truck and 6 gnomes</em></p>
  <p><strong>What I need to do is:</strong></p>
  <ol>
  <li>Identify is a string has 3 or more numbers.</li>
  <li>If so, skip the first number in the string.</li>
  <li>Find the next occurring number that is not the last one.</li>
  <li>Add &quot; ,&quot; to it.</li>
  </ol>
  <p>I can successfully go through the string and count the numbers it has in it:</p>
  <pre><code>    // count digits in string
      int digitsInString = 0;
      for (int i = 0, len = concatenatedValue.length(); i &lt; len; i++) {
          if (Character.isDigit(concatenatedValue.charAt(i))) {
              digitsInString++;
          }
      }
  
      // if it only has &lt; 3 digits, no formatting is needed
      if(digitsInString &lt; 3) {
          return concatenatedValue;
      } else {
      //I've got to figure out what goes here
      }
  </code></pre>
  <p>Any ideas? I'm writing it now by just splitting the string into a char array, but that seems kind of verbose and amateur. I'd like to maybe do something a little more efficient.</p>