String outof bound range-1

HI,

I am trying to generate this following code but I am getting following errors. My idea was to return the token as arralist but I do not know why I am getting this kind of errors.Could you please give me some suggestion or help on this problem.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

at java.lang.String.substring(String.java:1932)

at java.lang.String.substring(String.java:1905)

at TokenFile.getParentLabel(TokenFile.java:26)

at DEL.getTokenSenses(DEL.java:65)

at DEL.main(DEL.java:98)

public class TokenFile {

ArrayList<String> al=new ArrayList<String>();

public ArrayList<String> getParentLabel(String file) throws IndexOutOfBoundsException, IOException {

Label in=new Label();

Stack stack = new Stack();

String lastToken=null;

BufferedReader br = new BufferedReader(new FileReader(file)); // read text file by buffered reader

String line;

while ((line = br.readLine()) != null) {

String lastPath = line.substring( line.lastIndexOf("/"));

lastToken=lastPath.substring(1,lastPath.length());

al.add(lastToken);// add token to arraylist

System.out.println("ess="+al);

}

br.close();

return al;

}

}

[1335 byte] By [morshed_1471@yahoo.coma] at [2007-11-27 9:59:50]
# 1
I would suggest that your problem is on TokenFile.java line 26 (see the exception) where you are using lastIndexOf() which returns -1 if it does not find the target.P.S. Exception stack traces are a great help - if you read them.
sabre150a at 2007-7-13 0:30:55 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi

You can modify ur code as follows to get ride of the Exception..

It waz a Logical Error

public class TokenFile {

ArrayList<String> al=new ArrayList<String>();

public ArrayList<String> getParentLabel(String file) throws IndexOutOfBoundsException, IOException {

Label in=new Label();

Stack stack = new Stack();

String lastToken=null;

BufferedReader br = new BufferedReader(new FileReader(file)); // read text file by buffered reader

String line;

while ((line = br.readLine()) != null) {

//String lastPath = line.substring( line.lastIndexOf("/"));

String lastPath = line.substring( (line.lastIndexOf("/") == -1) ? 0 : line.lastIndexOf("/"));

lastToken=lastPath.substring(1,lastPath.length());

al.add(lastToken); // add token to arraylist

System.out.println("ess="+al);

}

br.close();

return al;

}

}

Regards

mohammedsajida at 2007-7-13 0:30:55 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks Dear Sajid . It was logical error. it is working fine now.. ...morshed
morshed_1471@yahoo.coma at 2007-7-13 0:30:55 > top of Java-index,Java Essentials,Java Programming...