This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes please explain the following method, please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please explain the following method, please" Watch "please explain the following method, please" New topic

please explain the following method, please

tim hunt

Joined: Apr 19, 2001
Posts: 9
private void skipDelimiters() {
while (!retTokens &&
(currentPosition < maxPosition) &&<br /> (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {

I have taken the above method from the StringTokenizer class and am trying to understand how it works. As I see it the loop breaks only if currentpos is greater than maxposition and if -1 is returned by the indexOf method. This is the bit I dont understand, when does the method return -1? Your help is greatly appreciated.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Tim.
indexOf is a method of the java.lang.String class. (You can read about it here.) indexOf returns -1 if the char passed as its argument does not appear in the instance of String indexOf is acting upon.
So this bit of StringTokenizer is looping through the String you are tokenizing, starting from an identified position in the String. Flow control depends on whether the character at the identified position is a delimiter for this StringTokenizer.
Hope this helps,
[This message has been edited by Art Metzer (edited June 24, 2001).]
I agree. Here's the link:
subject: please explain the following method, please