Class JlsTokenizer
Object
JlsTokenizer
Providing much the same functionality as java.util.StringTokenizer, but avoiding some of its more common problems. In
particular:
-
x,,y
yields a blank token between x and y, whereas with StringTokenizer it is swallowed. -
,a,
yields a blank token both before and after a, whereas with StringTokenizer both are swallowed.
This class is deliberately not a subclass of StringTokenizer as the behaviour is significantly different. It can, however, be used in many places where StringTokenizer is appropriate.
- Author:
- JSkeet
-
Constructor Summary
ConstructorsConstructorDescriptionJlsTokenizer(String str) Constructs a string tokenizer for the specified string.JlsTokenizer(String str, String delim) Constructs a string tokenizer for the specified string. -
Method Summary
Modifier and TypeMethodDescriptionintCalculates the number of times that this tokenizer'snextTokenmethod can be called before it generates an exception.booleanReturns the same value as thehasMoreTokensmethod.booleanTests if there are more tokens available from this tokenizer's string.Returns the same value as thenextTokenmethod, except that its declared return value isObjectrather thanString.Returns the next token from this string tokenizer.Returns the rest of the string.
-
Constructor Details
-
JlsTokenizer
Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is" \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.- Parameters:
str- a string to be parsed.
-
JlsTokenizer
Constructs a string tokenizer for the specified string. The characters in thedelimargument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.- Parameters:
str- a string to be parsed.delim- the delimiters.
-
-
Method Details
-
hasMoreTokens
public boolean hasMoreTokens()Tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.- Returns:
trueif and only if there is at least one token in the string after the current position;falseotherwise.
-
nextToken
Returns the next token from this string tokenizer.- Returns:
- the next token from this string tokenizer.
- Throws:
NoSuchElementException- if there are no more tokens in this tokenizer's string.
-
remainingToken
Returns the rest of the string.- Returns:
- the rest of the string.
- Throws:
NoSuchElementException- if there are no more tokens in this tokenizer's string.
-
hasMoreElements
public boolean hasMoreElements()Returns the same value as thehasMoreTokensmethod. It exists so that this class can implement theEnumerationinterface.- Returns:
trueif there are more tokens;falseotherwise.
-
nextElement
Returns the same value as thenextTokenmethod, except that its declared return value isObjectrather thanString. It exists so that this class can implement theEnumerationinterface.- Returns:
- the next token in the string.
- Throws:
NoSuchElementException- if there are no more tokens in this tokenizer's string.
-
countTokens
public int countTokens()Calculates the number of times that this tokenizer'snextTokenmethod can be called before it generates an exception. The current position is not advanced.- Returns:
- the number of tokens remaining in the string using the current delimiter set.
- See Also:
-
uk.org.skeet.util.JlsTokenizer#nextToken()
-