Solved

String Index out of range Error

Posted on 2003-11-26
6
520 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now