Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String Index out of range Error

Posted on 2003-11-26
6
Medium Priority
?
532 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
CEHJ earned 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

926 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