dirdir69
dirdir69

Reputation: 47

String Index OutOfBOunds Exception

public static void main(String [] args) throws IOException{
    BufferedReader in = new BufferedReader(new FileReader(".../senses/command_list"));
    String line = null;

    while((line = in.readLine()) != null){
        if(line.isEmpty()){
            continue;
        }
        line = line.trim();
        line = line.substring(2 + line.indexOf(">") , line.indexOf("))"));
        System.out.println(line);

    }

Following is an extract of the file

en_actions_.add(new ClusterEntry<String>("photography",-1, 2, 620554,"photography",null));
en_actions_.add(new ClusterEntry<String>("diagnostic procedure",-1, 1, 177127,"diagnostic procedure",null));
en_actions_.add(new ClusterEntry<String>("emergency procedure",-1, 1, 177783,"emergency procedure",null));
en_actions_.add(new ClusterEntry<String>("medical procedure",-1, 1, 1024392,"medical procedure",null));
en_actions_.add(new ClusterEntry<String>("process",-1, 5, 5470189,"process",null));

When I run this program, I encounter a String out of bounds exception with the following error message

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
and it points to the line with the indexOf operator.

Please let me know what I have done wrong.BTW the purpose, of the program is to store each field in an array of structures/classes. This is just the first part of the program.

Upvotes: 0

Views: 125

Answers (4)

HSR
HSR

Reputation: 11

2 things can happen:

  1. line.indexOf(")") will return -1 if String(line) doesnot contain ')'

  2. line.indexOf(")") < line.indexOf(">") if ')' appears before '>' in String(line)

and substring(begin_index,end_index) function will throw IndexOutOfBoundsException if beginIndex is larger than endIndex

Upvotes: 1

Shrikant Kakani
Shrikant Kakani

Reputation: 1561

Well I don't know about what kind of String(line) your file has but it might happen that you get a String of length 1 in which case the following line would produce the exception.

line = line.substring(2 + line.indexOf(">") , line.indexOf(")"));

Moreover, if your String(line) doesn't have ')', then in that case line.indexOf(")") will give -1 which makes no sense.

Also, try the following code yourself and the same exception as yours will reproduce.

String x = "a";
System.out.println(x.substring(2 + x.indexOf(">") , x.indexOf(")")));

In String x, store any String that doesn't contains both '>' and ')'.

Upvotes: 3

Kent
Kent

Reputation: 195269

the cause of your exception is relatively clear. back to your requirement, I think it is typical regex use case. Because

  • it saves you from indexOf() calculation.
  • it saves the error handling part, E.g. the index=-1 case
  • depends on the jdk version you used, if <7, subString() has potential mem leak problem. with regex, you don't have that risk.

.

String s = "en_actions_.add(new ClusterEntry<String>(\"photography\",-1, 2, 620554,\"photography \",null));";
        Pattern p = Pattern.compile("(?<=>\\()[^)]*");
        Matcher m = p.matcher(s);
        if (m.find()) {
            System.out.println(m.group());
        }

above codes has a look-behind regex, to get text between >( and ), if you like you can added <string>( as look-behind pattern.

I would remove the answer, if it is kind of off topic.

Upvotes: 1

dimoniy
dimoniy

Reputation: 5995

Looks like one of the lines in the file doesn't have a > or ) character. In fact I don't see the > in your file data at all. One of the operators might be multiline. Extract the indexOf into separate variables and check that those symbols are there.

int index1 = line.indexOf(">");
if (index1 < 0) {
 //skip this line
 continue;
}

Also, you might want to swap the trim and isEmpty check so it would filter out lines consisting of just whitespaces.

Upvotes: 1

Related Questions