Solved

String Index out of range Error

Posted on 2003-11-26
6
522 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 35
eclipse apache tomcat admin console 52 123
spring jars download 1 36
What browser will run Java? 7 69
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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