Solved

String Index out of range Error

Posted on 2003-11-26
6
521 Views
Last Modified: 2008-03-17
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,endposition);"
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.getSearchGroup(),0);

//sort the string
Arrays.sort(gstr, String.CASE_INSENSITIVE_ORDER);

if(gstr != null){
response.write("<script>alert(\"gstr:"+gstr.length+"\")</script>");
      
for(int i=0; i<gstr.length; i++){
response.write("<script>alert(\"i :"+i+"\")</script>");
endposition = gstr[i].indexOf(',');
String group = gstr[i].substring(3,endposition);

//if group starts with sapportal groups , add it to vector
if(group.startsWith("SAPPortal")){
response.write("<script>alert(\"grp:"+group+"\")</script>");
vsap.addElement(group);
}                              
}      
setVSap(vsap);
}//if gstr != null
else{
response.write("<script>alert(\"String [] grps is null\")</script>");
}      
                  
}//end try
catch(Exception e){
response.write("<script>alert(\"issues:"+e.getMessage()+"\")</script>");
}

Thanx
SB
0
Comment
Question by:ScarletBlue
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9824040
You must check it's not out of range.

if (endposition < 0)
    return;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9824060
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
0
 

Author Comment

by:ScarletBlue
ID: 9824072
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?
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 9824086
Well, the first suggestion was not a 'working' suggestion - just an illustration. My second suggestion is better.

endposition = gstr[i].indexOf(',');
if (endposition < 0)
    endposition = gstr[i].length();

or

endposition = gstr[i].indexOf(',');
endposition = endposition < 0? gstr[i].length() : endposition;

0
 

Author Comment

by:ScarletBlue
ID: 9824721
thank u cehj, it works!
have a lovely day
SB
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9824790
:-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question