Class JlsTokenizer

Object
JlsTokenizer

public class JlsTokenizer extends Object
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.
Some pieces of StringTokenizer aren't supported, such as returning delimiters and changing delimiters during operation.

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 Details

    • JlsTokenizer

      public JlsTokenizer(String str)
      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

      public JlsTokenizer(String str, String delim)
      Constructs a string tokenizer for the specified string. The characters in the delim 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

      public String 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

      public 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 the hasMoreTokens method. It exists so that this class can implement the Enumeration interface.
      Returns:
      true if there are more tokens; false otherwise.
    • nextElement

      public Object nextElement()
      Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration 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's nextToken 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()