java - StringIndexOutOfBounds by -3 -
i have inherited project should work. however, original coder unavailable.
i'm getting stringindexoutofboundsexception
.
the stack trace goes line:
int lserver_num = integer.parseint(motd.substring(motd.indexof("-") + 1, motd.indexof(" ")));
suggestions?
you trying substring value greater string's length or less 0. if provide more code, easier diagnose problem exactly. haven't, have assume.
here example:
string test = "123-2 "; int = integer.parseint(test.substring(test.indexof("-") + 1, test.indexof(" "))); system.out.println(i);
when run it, prints out 2, no problem. if remove space string, gives me arrayindexoutofboundsexception: -5
dont know how arrives @ -5, can see string 5 characters long without space, guess defaults strings length if cant find character, maybe?
from this, have ask: sure string contains blank space
or hyphen
?
check with
if (motd.contains(" ") && motd.contains("-")
also null check see if motd null first.
Comments
Post a Comment