StringTokenizer Methods in Java with Examples | Set 2
StringTokenizer class in Java is used to break a string into tokens. One must go through StringTokenizer class where concepts and constructors are discussed which help in better understanding methods that are been discussed here below as follows:
Methods of StringTokenizer class are as follows:
- hasMoreToken
- nextToken
- countTokens
- nextElement
- hasMoreElements
Method 1: hasMoreTokens()
The method 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.
Syntax:
public boolean hasMoreTokens()
Return Type: A boolean value, true if and only if next token to the current position in the string exists, else false.
Method 2: nextToken()
The method returns the next token from the given StringTokenizer.
Syntax:
public String nextToken()
Return Type: The next token from the given StringTokenizer if present.
Exception Thrown: NoSuchElementException if no more tokens are left.
Method 3: countTokens()
The method returns the total number of tokens present so that we can use nextToken() method before it gives an exception.
Syntax:
public int countTokens()
Return Type: the number of tokens remaining in the string using the current delimiter set.
Example
Java
import java.util.*;
public class GFG {
public static void main(String args[]) {
String mydelim = " : " ;
String mystr = "JAVA : Code : String : Tokenizer : Geeks" ;
StringTokenizer geeks3 =
new StringTokenizer(mystr, mydelim);
int count = geeks3.countTokens();
System.out.println( "Number of tokens : " + count + "\n" );
for ( int i = 0 ; i < count; i++)
System.out.println( "token at [" + i + "] : "
+ geeks3.nextToken());
while (geeks3.hasMoreTokens())
System.out.println(geeks3.nextToken());
}
}
|
Output
Number of tokens : 5
token at [0] : JAVA
token at [1] : Code
token at [2] : String
token at [3] : Tokenizer
token at [4] : Geeks
Method 4: nextElement()
The method works similar to nextToken except that it returns an Object rather than String. Exists so that this class can implement the Enumeration interface.
Syntax:
public Object nextElement()
Return Type: the next token from the given StringTokenizer.
Exception Thrown: NoSuchElementException if there are no more tokens left.
Method 5: hasMoreElements()
This method returns the same value as hasMoreToken. It exists so that the class can implement the Enumeration interface.
Syntax:
public boolean hasMoreElements()
Return Type: A boolean value, true if tokens are present in the string, else false
Example
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
String mydelim = " : " ;
String mystr
= "JAVA : Code : String : Tokenizer : Geeks" ;
StringTokenizer geeks
= new StringTokenizer(mystr, mydelim);
int count = geeks.countTokens();
System.out.println( "Number of tokens : " + count);
while (geeks.hasMoreElements())
System.out.println(geeks.nextElement());
}
}
|
Output
Number of tokens : 5
JAVA
Code
String
Tokenizer
Geeks
Tip: Do remember certain important points as listed:
- 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.
Last Updated :
30 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...