ScarletBlue
asked on
String Index out of range Error
Hi all
I have an error "String index out of range: -4". What does this error mean??
The error seems to be in this line of code "String group = gstr[i].substring(3,endpos ition);"
There are 21 groups that must be added to Vector vsap, but on the 20th time in the loop, it displays that error.
Here is my code
try{
//get all grps in portal, return in string[]
gstr = igf.findGroups(igf.getSear chGroup(), 0);
//sort the string
Arrays.sort(gstr, String.CASE_INSENSITIVE_OR DER);
if(gstr != null){
response.write("<script>al ert(\"gstr :"+gstr.le ngth+"\")< /script>") ;
for(int i=0; i<gstr.length; i++){
response.write("<script>al ert(\"i :"+i+"\")</script>");
endposition = gstr[i].indexOf(',');
String group = gstr[i].substring(3,endpos ition);
//if group starts with sapportal groups , add it to vector
if(group.startsWith("SAPPo rtal")){
response.write("<script>al ert(\"grp: "+group+"\ ")</script >");
vsap.addElement(group);
}
}
setVSap(vsap);
}//if gstr != null
else{
response.write("<script>al ert(\"Stri ng [] grps is null\")</script>");
}
}//end try
catch(Exception e){
response.write("<script>al ert(\"issu es:"+e.get Message()+ "\")</scri pt>");
}
Thanx
SB
I have an error "String index out of range: -4". What does this error mean??
The error seems to be in this line of code "String group = gstr[i].substring(3,endpos
There are 21 groups that must be added to Vector vsap, but on the 20th time in the loop, it displays that error.
Here is my code
try{
//get all grps in portal, return in string[]
gstr = igf.findGroups(igf.getSear
//sort the string
Arrays.sort(gstr, String.CASE_INSENSITIVE_OR
if(gstr != null){
response.write("<script>al
for(int i=0; i<gstr.length; i++){
response.write("<script>al
endposition = gstr[i].indexOf(',');
String group = gstr[i].substring(3,endpos
//if group starts with sapportal groups , add it to vector
if(group.startsWith("SAPPo
response.write("<script>al
vsap.addElement(group);
}
}
setVSap(vsap);
}//if gstr != null
else{
response.write("<script>al
}
}//end try
catch(Exception e){
response.write("<script>al
}
Thanx
SB
This may fix it:
if (endposition < 0)
endposition = gstr[i].length();
>>if(gstr != null)
is redundant btw. You won't be able to sort a null array without an exception
if (endposition < 0)
endposition = gstr[i].length();
>>if(gstr != null)
is redundant btw. You won't be able to sort a null array without an exception
ASKER
I have my code in a function that returns a vector..
i have inserted the code you have suggested in the for loop after the if statement..
but it gives an error wanting it to return a vector....
what would i return?
i have inserted the code you have suggested in the for loop after the if statement..
but it gives an error wanting it to return a vector....
what would i return?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank u cehj, it works!
have a lovely day
SB
have a lovely day
SB
:-)
if (endposition < 0)
return;