Hi everyone, I am getting an exception on the following code and not sure why. Even in debug, there is nothing that indicates that the start position is out of bounds (start is at 128 while length is at 159 when the exception is thrown). The exception will follow the code.
public class TestStringBuffer {
public static void main(String[] args) {
String emptyString = new String(new byte[159]);
int[] sizes = new int[] {64, 64, 4, 1, 8, 10, 8};
String[] fields = new String[] {"test1", "test2", "ABC", "X", "ABCDEF", "123456", "qwerty" };
int start = 0;
StringBuilder newRec = new StringBuilder(emptyString);
for (int i = 0; i < fields.length; i++) {
String field = fields[i];
int end = start + sizes[i];
newRec.replace(start, end, field);
start = end;
}
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start > length()
at java.lang.AbstractStringBuilder.replace(Unknown Source)
at java.lang.StringBuilder.replace(Unknown Source)
at mycode.TestStringBuffer.main(TestStringBuffer.java:17)
Thanks in advance.
[ October 09, 2008: Message edited by: Patrick Williams ]