Results 1 to 2 of 2
Thread: Help regarding indexOf
- 07-10-2007, 12:32 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 1
- Rep Power
- 0
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(String.java:1938)
at TestClass.main(TestClass.java:21)
here is my code.......
====== the output ========Java Code:// This file uses extractor.java 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. import java.io.*; public class TestClass { public static void main(String [] args) { extractor ext = new extractor(); String [] result = ext.extract("myFile.txt"); try { int i; for(i=0;result[i]!=null;i=i+3) { 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)); System.out.println(Authors); System.out.println(Title); System.out.println(Journal); }//end of for. } //end of try. catch(Exception ex) { ex.printStackTrace(); } } }
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(String.java:1938) at TestClass.main(TestClass.java:21) Press any key to continue...
- 07-10-2007, 01:12 PM #2
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
-
tricky indexOf implementation -- Help!!
By definewebsites in forum New To JavaReplies: 3Last Post: 12-10-2007, 12:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks