Help regarding
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 isjava.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.......
// 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.*;
publicclass TestClass
{
publicstaticvoid 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();
}
}
}
====== the output ========
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...

