title: Index out of range
body: <p>I want my program to mingle to strings in an array. The strings are coming from a .dat file. I keep getting an index out of range error. </p>
  
  <p>the input file :
  3
  xyz abc
  abc rstuvwxy
  rstuv ab</p>
  
  <p>wanted output:
  axbycz
  rasbtcuavbwcxayb
  rasbtaubva</p>
  
  <p>error i'm getting:</p>
  
  <p>arbsctException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
      at java.lang.String.charAt(Unknown Source)
      at test.main(test.java:39)</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  
  public class test {
  
      public static void main(String[] args) throws FileNotFoundException {
  
          File file = new File("strings.dat");
          Scanner infile = new Scanner(file);
          String string1;
          String[] mingle = new String[2];
          int length;
  
          infile.nextLine();
  
          while (infile.hasNextLine()) {
              string1 = infile.nextLine();
  
              for (int i = 0; i &lt; mingle.length; i++) {
                  mingle = string1.toLowerCase().split("[\\s.]");
              }
  
              System.out.println(mingle[1] + mingle[0]);
  
              if (mingle[0].length() &gt; mingle[1].length()) {
                  length = mingle[0].length();
              }
  
              else if (mingle[1] == mingle[0]) {
                  length = mingle[1].length();
              }
  
              else {
                  length = mingle[1].length();
              }
  
              for (int i = 0; i &lt; length; i++) {
                  System.out.print(mingle[0].charAt(i % length));
                  System.out.print(mingle[1].charAt(i % length));
              }
          }
          infile.close();
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>Subsequent Error</h2>
  
  <blockquote>
    <p>arbsctException in thread "main"
    java.lang.StringIndexOutOfBoundsException: String index out of range:
    3 at java.lang.String.charAt(Unknown Source) at
    test.main(test.java:39)</p>
  </blockquote>