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.
the input file : 3 xyz abc abc rstuvwxy rstuv ab
wanted output: axbycz rasbtcuavbwcxayb rasbtaubva
error i'm getting:
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)
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 < mingle.length; i++) {
mingle = string1.toLowerCase().split("[\\s.]");
}
System.out.println(mingle[1] + mingle[0]);
if (mingle[0].length() > 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 < length; i++) {
System.out.print(mingle[0].charAt(i % length));
System.out.print(mingle[1].charAt(i % length));
}
}
infile.close();
}
}
Subsequent Error
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)