0
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.

6
  • for sure the problem is with your substring call. this error happens when you excedes the maximum length for the string. check if that part(2+ line.indexOf(">")) is not greater than the total length of the string Commented Jun 2, 2014 at 21:24
  • Try surrounding the body of the loop with a try...catch block that prints the offending line to standard error. Commented Jun 2, 2014 at 21:25
  • It prints most of the lines. The exception pops up somewhere in the middle. Commented Jun 2, 2014 at 21:26
  • maybe not related to your question, but why not extracting the text via regex? it saves the index calculations... Commented Jun 2, 2014 at 21:27
  • @Kent i am not familiar with the working of Regex.. I should really learn that. Commented Jun 2, 2014 at 21:31

4 Answers 4

3

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 ')'.

Sign up to request clarification or add additional context in comments.

1 Comment

Every line in the file is of the format I have specified in the question. I am pretty sure there is no line that has a string of length 1.
1

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.

3 Comments

> character is in <String>. I will try swapping that.
Swapping the trim and isEmpty commands worked, but I have no idea why. Could you tell me how? Thank You !
@user3645570 Imagine that you had a line with a bunch of whitespaces in the end of the file, or the line with non-printable characters. If you test the line with isEmpty it will not work because string is technically not empty. If you do trim first those characters will be removed and isEmpty will do what it supposed.
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.