StringTokenizer methods in Java with example : / / | \ \ hasMoreToken nextToken countTokens nextElement hasMoreElements
We strongly recommend to refer below post as a prerequisite for this.
StringTokenizer class in Java with example | Set 1
Following are the StringTokenizer class methods :
- hasMoreTokens(): The method java.util.StringTokenizer.hasMoreTokens() plays role in testing, if tokens are present for the StringTokenizer’s string.
Those characters that are considered to be delimiters by the StringTokenizer object are changed to characters in the string delimiter. Then the next token to the current position in the string is returned.
public boolean hasMoreTokens() Returns: True if and only if next token to the current position in the string exists, else false.
- nextToken(): The method java.util.StringTokenizer.nextToken() returns next token from the given StringTokenizer.
public String nextToken() Return: the next token from the given StringTokenizer if present. Throws: NoSuchElementException - if no more token are left.
- countTokens(): The method java.util.StringTokenizer.countTokens() returns total number of tokens present so that we can use nextToken() method before it gives an exception..
public int countTokens() Return : the number of tokens remaining in the string using the current delimiter set.
Number of tokens : 5 token at  : JAVA token at  : Code token at  : String token at  : Tokenizer token at  : Geeks
- nextElement(): The method java.util.StringTokenizer.nextElements() works similar to nextToken except that it returns Object rather than String.
Exists so that this class can implement the Enumeration interface.
public Object nextElement() Return: the next token from the given StringTokenizer. Throws:NoSuchElementException - if there are no more tokens left.
- hasMoreElements(): This method java.util.StringTokenizer.hasMoreElements() returns same value as hasMoreToken. It exists so that the class can implement the Enumeration interface.
public boolean hasMoreElements() Return: true if tokens are present in the string, else false
Number of tokens : 5 JAVA Code String Tokenizer Geeks
- counttoken() method is a good alternative in using the combination hasMoreTokens and nextToken().
- The combination of countTokens and nextToken is used if you are interested in the number of tokens also.
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- StringTokenizer countTokens() Method in Java with Examples
- StringTokenizer nextToken() Method in Java with Examples
- StringTokenizer hasMoreElements() Method in Java with Examples
- StringTokenizer nextElement() Method in Java with Examples
- StringTokenizer hasMoreTokens() Method in Java with Examples
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Java.util.BitSet class methods in Java with Examples | Set 2
- DoubleBuffer put() methods in Java with Examples | Set 1
- Abstract Methods in Java with Examples
- ByteBuffer put() methods in Java with Examples | Set -1
- FloatBuffer put() methods in Java with Examples
- FloatBuffer get() methods in Java with Examples
- DoubleBuffer get() methods in Java with Examples
- Buffer reset() methods in Java with Examples
- FloatBuffer clear() methods in Java with Examples