StringTokenizer in java- Constructor, method with example

1 What is StringTokenizer?

A StringTokenizer is a class in Java. It is used to divide strings in form of token based on some delimiters.

It is defined in java.util.stringtokenizer

2 StringTokenizer Constructors

Sr NoConstructor and Description
1StringTokenizer(String str) Construct a string tokenizer for given string
2StringTokenizer(String str, String delim) Construct a string tokenizer for given string and generate tokens based on delimiters
3StringTokenizer(String str, String delim, boolean returnDelims) Construct a string tokenizer for given string and generate tokens based on delimiters the returnDelims specify whether to include delim in token or not

Creating StringTokenizer object using first constructor

Creating StringTokenizer object using second constructor

3 StringTokenizer methods

A Java StringTokenizer countTokens() Method

This method is used to count the number of the tokens from current token position to the last token

Signature

public int countTokens()

Parameters

NA

Return

Return the number of tokens available from specific token position

Example

Initially token position was before the first word “Arise” so on calling st.countToken() returns 11.
on calling st.nextToken() position of token is incresed by one and it is on “Arise” from this position new token count is 10.

B Java StringTokenizer hasMoreElements() Method

This method checks for next token in StringTokenizer if the token exists then return true else return false. Work similar as
hasMoreToken()

Parameters

NA

Return

Return true if next token exists else return false

Example

Result

At starting object st is pointing before the first word(Java). on calling st.hasMoreElements it is checkin that the first word java is there so it is returning true.

C Java StringTokenizer hasMoreToken() Method

This method is used with StringTokenizer object to check whether there is more token or not
For example
StringTokenizer st=new StringTokenizer(“Hello Friends”);
There are two tokens Hello and Friends. At starting hasMoreToken() point before the Hello Token so calling this method will return true at first.
To get next token we use nextToken().
After getting token Friends, again calling hasMoreToken() it return false.

Signature

public boolean hasMoreToken()

Parameters

NA

Return

Return true if there is more token else return false

Example

Result

St object is pointing before the Hello so calling st.hasMoreTokens() will always return true.

D Java StringTokenizer nextElement() Method

This method returns next token from stringTokenizer object this method work same as nextToken() only the return type of this method is object instead of String

Signature

public Object nextElement()

Parameters

NA

Return

Return next token from StringTokenizer object

Throws

NoSuchElementException – If there is no token in StringTokenizer object then it throw this exception

Example

Lets see how to tokenize a string in java

Another Example

E Java StringTokenizer nextToken() Method

This method returns next token from stringTokenizer object

Signature

public String nextToken()

Parameters

NA

Return

Return next token from StringTokenizer object

Throws

NoSuchElementException – If there is no token in StringTokenizer object then it throw this exception

Example

Result