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 the 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 an 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 the 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
- countTokens() 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 firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- StringTokenizer countTokens() Method in Java with Examples
- StringTokenizer hasMoreElements() Method in Java with Examples
- StringTokenizer hasMoreTokens() Method in Java with Examples
- StringTokenizer nextToken() Method in Java with Examples
- StringTokenizer nextElement() Method in Java with Examples
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Static methods vs Instance methods in Java
- Java.util.BitSet class methods in Java with Examples | Set 2
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- BitSet class methods in Java with Examples | Set 3
- Math class methods in Java with Examples | Set 2
- DoubleBuffer put() methods in Java with Examples | Set 1
- ByteBuffer put() methods in Java with Examples | Set -1
- ArrayList and LinkedList remove() methods in Java with Examples
- util.date class methods in Java with Examples
- FloatBuffer get() methods in Java with Examples
- FloatBuffer put() methods in Java with Examples
- DoubleBuffer get() methods in Java with Examples
- ByteBuffer putFloat() methods in Java with Examples
Improved By : pavan singh