This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error" Watch "error" New topic
Author

error

kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
Hi,
I'm getting this error:
java.lang.StringIndexOutOfBoundsException: String index out of
range: -1
What causes this error to occur? TIA.
Kevin
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Please show us the code that you were executing when you got this error. It LOOKS like you were doing a subString or messing with a character in the String and used an index past the length of the String.


"JavaRanch, where the deer and the Certified play" - David O'Meara
kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
This is the code:
String infoST = "";
BufferedReader inPDF = new BufferedReader(new FileReader(fName));
String info, allInfo = new String();
while((info = inPDF.readLine()) != null) {
allInfo += info;
}
String ends = allInfo.substring(allInfo.lastIndexOf("Rich"), allInfo.length());
inPDF.close();
StringTokenizer tempST = new StringTokenizer(ends, "/");
infoST = tempST.nextToken();

[This message has been edited by kevin schmidt (edited October 05, 2001).]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Kevin
Your problem is probably in the line:
String ends = allInfo.substring(allInfo.lastIndexOf("Rich"), allInfo.length());
The 'lastIndexOf("Rich")' is probably returning -1 (meaning there was no 'Rich' in the string. You should break it out of the substring call and test the return value, something like this would do it:

also, I assume that your catching the possible IOException from your readLine ?!?
Hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error
 
Similar Threads
Getting error in setupConnection()
problem in retreiving value
Regarding tomcat
print statement in the declaration ...
WA #1.....word association