title: How to convert a StringBuilder of integers into an array of ints?
body: <p>So for an assignment I had to write a method that would take two arrays and interleave them. I chose to convert those arrays to strings, take substrings of each string and add them to a stringbuilder. Now I should have a stringbuilder full of integers from both arrays, but the method the teacher wants us to create must return an array of ints. How can I convert this stringbuilder to an array of ints?</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>public static int[] interleaveArrays(int[] array1, int[] array2, int stride, int maxElements){
  
      if (stride &lt;= 0){
          return null;
      }
      if (maxElements &lt;= 0){
          return null;
      }
  
      String arrayA = array1.toString();
      String arrayB = array2.toString();
      StringBuilder sb = new StringBuilder(array1.length + array2.length);
  
      boolean done = false;
      boolean end = false;
      int i = 0;
      int j = 0;
  
      while (done = false){//create a substring of the first array &amp; append to stringbuilder
          String newArray1 = arrayA.substring(i, i + (stride - 1));
          sb.append(newArray1);
          if (i &gt; arrayA.length()){
              done = true;
          }
          while (end = false){//create a substring of the second array &amp; append to stringbuilder
              String newArray2 = arrayB.substring(j, (j + stride - 1));
              sb.append(newArray2);
              if (j &gt; arrayB.length()){
                  end = true;
              }
          }
          if (sb.length() &gt;= maxElements){
              done = true;
          }
      }
  }
  </code></pre>
  
  <p>(side note:</p>
  
  <p><strong>stride</strong> is the amount of integers that should be taken at a time from each array. [e.g. if you have a stride of 2 and the arrays <strong>{1, 3, 7, 2}</strong> and <em>{5, 2, 9, 6}</em>, the array returned should be {<strong>1, 3,</strong> <em>5, 2,</em> <strong>7, 2,</strong> <em>9, 6</em>}]</p>
  
  <p><strong>maxElements</strong> is the maximum number of integers allowed in the returned array)</p>