Results 1 to 2 of 2
  1. #1
    gauravj is offline Member
    Join Date
    Jul 2007
    Rep Power

    Smile Help regarding indexOf

    Hi, i am New to java and I am parsing a file.The file looks like
    =========== file starts ================================
    1: Kotloff KL. <-- authors
    Bacterial diarrheal pathogens. <-- Title of the paper
    Scand J Infect Dis. <-- Journal

    2: Ivanoff B.
    [Traveller's diarrhea: which vaccines]
    J Pak Med Assoc.

    3: Fournier JM.
    [The current status of research on a cholera vaccine?]
    J Pharm Sci.

    4: Keddy KH, Koornhof HJ.
    Cholera--the new epidemic?.
    S Afr Med J.
    =========== file ends ================================
    The problem i am facing here is that when i am using the "indexOf" for the string to specify ....for the "title of paper"...there are different ending criteria. Is there any why i can specify that the ending criteria is either a " . "(dot) or "]" or a "?". here is my code which works fine if the ending criteria is a "."(dot).but when it comes to " ] " its giving an error which is java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(
    at TestClass.main(

    here is my code.......
    Java Code:
    // This file uses file to format the text file 'result.txt'
    // method ext.extract returns the formatted array of strings which I write
    // to 'result.txt' file with newline character(\r\n) added.
    public class TestClass
      public static void main(String [] args)
        extractor ext = new extractor();
        String [] result = ext.extract("myFile.txt");
         int i;
           int index=0;
           String Authors =result[i].substring((result[i].indexOf(':')+3),(result[i].indexOf('.')+1));
           String Title = result[i+1].substring(index,(result[i+1].indexOf('.')));
           String Journal = result[i+2].substring(index,(result[i+2].indexOf('.')+1));
         }//end of for.
        } //end of try.
        catch(Exception ex)
    ====== the output ========

    Java Code:
    Kotloff KL. 
    Bacterial diarrheal pathogens.
    Scand J Infect Dis. 
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
            at java.lang.String.substring(
            at TestClass.main( 
    Press any key to continue...

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Rep Power


    Since the end character can be any of " . "(dot) or "]" or a "?", you need to make sure that you call substring method if the returned value of indexOf is not -1. When indexOf method returns -1, that means the string is not found in the source string and when you try to get substring starting from -1, you get this error, since the minimum valid index is 0 for a string!

    So to solve this, first check if indexOf does not return -1 and if so get the substring.

Similar Threads

  1. tricky indexOf implementation -- Help!!
    By definewebsites in forum New To Java
    Replies: 3
    Last Post: 12-10-2007, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts