Java string too long?
Posted
by wrongusername
on Stack Overflow
See other posts from Stack Overflow
or by wrongusername
Published on 2010-05-06T04:08:07Z
Indexed on
2010/05/06
4:18 UTC
Read the original article
Hit count: 271
I have the following code in Java (which worked just fine in C++ for some reason) which produces an error:
int a;
System.out.println("String length: " + input.length());
for(a = 0; ((a + 1) * 97) < input.length(); a++) {
System.out.print("Substring at " + a + ": ");
System.out.println(input.substring(a * 97, 97));
//other code here...
}
Output:
String length: 340
Substring at 0: HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHe
Substring at 1:
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -97
//long list of "at ..." stuff
Substring at 2:
Using a string of length 200, however, the following output is produced:
String length: 200
Substring at 0: HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHe
Substring at 1:
That is it; no exceptions raised, just... nothing. What is happening here?
© Stack Overflow or respective owner