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 TypeMethodDescriptionint
Calculates the number of times that this tokenizer'snextToken
method can be called before it generates an exception.boolean
Returns the same value as thehasMoreTokens
method.boolean
Tests if there are more tokens available from this tokenizer's string.Returns the same value as thenextToken
method, except that its declared return value isObject
rather 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 thedelim
argument 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:
true
if and only if there is at least one token in the string after the current position;false
otherwise.
-
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 thehasMoreTokens
method. It exists so that this class can implement theEnumeration
interface.- Returns:
true
if there are more tokens;false
otherwise.
-
nextElement
Returns the same value as thenextToken
method, except that its declared return value isObject
rather thanString
. It exists so that this class can implement theEnumeration
interface.- 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'snextToken
method 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()
-