StringTokenizer in Java
The java.util.StringTokenizer class allows you to break a String into tokens. It is simple
way to break a String.
It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc.
In the StringTokenizer class, the delimiters can be provided at the time of creation or one
by one to the tokens.
Constructor Description
StringTokenizer(String str) It creates StringTokenizer with specified string.
StringTokenizer(String str, String delim) It creates StringTokenizer with specified string and delimiter.
It creates StringTokenizer with specified string, delimiter and
return Value. If returnvalue is true, delimiter characters are
StringTokenizer(String str, String delim,
considered to be tokens. If it is false, delimiter
boolean returnValue)
Characters serve to separate tokens.
Methods of the StringTokenizer Class
The six useful methods of the StringTokenizer class are as follows:
Methods Description
boolean hasMoreTokens() It checks if there is more tokens available.
It returns the next token from the StringTokenizer
String nextToken()
object.
String nextToken(String delim) It returns the next token based on the delimiter.
boolean hasMoreElements() It is the same as hasMoreTokens() method.
It is the same as nextToken() but its return type is
Object nextElement()
Object.
int countTokens() It returns the total number of tokens.
Example of StringTokenizer Class
Let's see an example of the StringTokenizer class that tokenizes a string "my name is
khan" on the basis of whitespace.
Example 1
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan"," ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
OUTPUT
my
name
is
khan
Example 2
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
OUTPUT
java -cp /tmp/cLPe24x1uX/Simple
my
name
is
khan
Example 3
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken(" "));
Output
java -cp /tmp/ov7pOmjS2f/Simple
my
name
is
khan
Example 4
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan","/");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken(" "));
Output
java -cp /tmp/rALr7kwTay/Simple
my
name
is
khan
Example 5
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my/name/is/khan","/");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken(" "));
}
OUTPUT
java -cp /tmp/wAGucBDOtZ/Simple
my/name/is/khan
Example 6
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my/name/is/khan","/");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
Output
java -cp /tmp/8UwQchViea/Simple
my
name
is
khan
Example 7
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my/name/is/khan","/");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken("/"));
}
Output
java -cp /tmp/zpmX3vr5AW/Simple
my
name
is
khan
Example 8
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my/name/is/khan","/");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
Output
java -cp /tmp/OmzpcZFQUN/Simple
my
name
is
khan
Example 9
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my/name/is/khan");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
Output
java -cp /tmp/YsIlKQvR7K/Simple
my/name/is/khan
Example 10
import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is khan");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
OUTPUT
java -cp /tmp/WQfW7XojTD/Simple
my
name
is
khan
All the above Java codes, demonstrates the use of StringTokenizer class and its
methods hasMoreTokens() and nextToken().
Example 10
Here we do not use loop
import java.util.*;
public class Test {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("my,name,is,khan");
// printing next token
System.out.println("Next token is : " + st.nextToken(","));
}
}
Output
java -cp /tmp/i9jHCZRZtA/Test
Next token is : my
Example 11
import java.util.*;
public class Test {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("my/name/is/khan");
// printing next token
System.out.println("Next token is : " + st.nextToken(","));
}
}
Output
java -cp /tmp/etqOqQN28n/Test
Next token is : my/name/is/khan
Example 12
import java.util.*;
public class Test {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("my/name/is/khan");
// printing next token
System.out.println("Next token is : " + st.nextToken("/"));
}
}
output
java -cp /tmp/dkTdHzN6M8/Test
Next token is : my
Example of hasMoreTokens() method of the StringTokenizer
class
This method returns true if more tokens are available in the tokenizer String otherwise
returns false.
import java.util.StringTokenizer;
public class StringTokenizer1
/* Driver Code */
public static void main(String args[])
/* StringTokenizer object */
StringTokenizer st = new StringTokenizer("Demonstrating methods from StringTokenizer
class"," ");
/* Checks if the String has any more tokens */
while (st.hasMoreTokens())
System.out.println(st.nextToken());
Output
java -cp /tmp/YbWIyMhLWm/StringTokenizer1
Demonstrating
methods
from
StringTokenizer
Class
Example of hasMoreElements() method of the StringTokenizer
class
Example1
import java.util.StringTokenizer;
public class StringTokenizer2
{
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer("Hello everyone I am a Java developer",
" ");
while (st.hasMoreElements())
{
System.out.println(st.nextToken());
}
}
}
Output
java -cp /tmp/5FCrut1ewC/StringTokenizer2
Hello
everyone
am
Java
Developer
Example 2
import java.util.StringTokenizer;
public class StringTokenizer2
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer("Hello everyone I am a Java developer");
while (st.hasMoreElements())
System.out.println(st.nextToken());
Output
java -cp /tmp/rmrQ7eNMlv/StringTokenizer2
Hello
everyone
am
Java
developer
Example of nextElement() method of the StringTokenizer class
nextElement() returns the next token object in the tokenizer String.
Example 1
import java.util.StringTokenizer;
public class StringTokenizer3
{
/* Driver Code */
public static void main(String args[])
{
/* StringTokenizer object */
StringTokenizer st = new StringTokenizer("Hello Everyone Have a nice day"," ");
/* Checks if the String has any more tokens */
while (st.hasMoreTokens())
{
/* Prints the elements from the String */
System.out.println(st.nextElement());
}
}
}
Output
java -cp /tmp/Gkjr52nO80/StringTokenizer3
Hello
Everyone
Have
nice
day
Example 2
import java.util.StringTokenizer;
public class StringTokenizer3
/* Driver Code */
public static void main(String args[])
/* StringTokenizer object */
StringTokenizer st = new StringTokenizer("Hello Everyone Have a nice day");
/* Checks if the String has any more tokens */
while (st.hasMoreTokens())
/* Prints the elements from the String */
System.out.println(st.nextElement());
}
Output
java -cp /tmp/SmzwJ7yJav/StringTokenizer3
Hello
Everyone
Have
nice
day
Example 3
import java.util.StringTokenizer;
public class StringTokenizer3
/* Driver Code */
public static void main(String args[])
/* StringTokenizer object */
StringTokenizer st = new StringTokenizer("Hello,Everyone,Have,a,nice,day");
/* Checks if the String has any more tokens */
while (st.hasMoreTokens())
/* Prints the elements from the String */
System.out.println(st.nextElement());
Output
java -cp /tmp/zHJbukdqrt/StringTokenizer3
Hello,Everyone,Have,a,nice,day
Example 4
import java.util.StringTokenizer;
public class StringTokenizer3
/* Driver Code */
public static void main(String args[])
/* StringTokenizer object */
StringTokenizer st = new StringTokenizer("Hello,Everyone,Have,a,nice,day",",");
/* Checks if the String has any more tokens */
while (st.hasMoreTokens())
/* Prints the elements from the String */
System.out.println(st.nextElement());
Output
java -cp /tmp/LIJ72PyKGz/StringTokenizer3
Hello
Everyone
Have
nice
day
Examples of Java String Tokenizer Constructors
// Java Program to implement
// Java String Tokenizer Constructors
import java.util.*;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
// Constructor 1
System.out.println("Using Constructor 1 - ");
// Creating object of class inside main() method
StringTokenizer st1 = new StringTokenizer(
"Hello Geeks How are you", " ");
// Condition holds true till there is single token
// remaining using hasMoreTokens() method
while (st1.hasMoreTokens())
// Getting next tokens
System.out.println(st1.nextToken());
// Constructor 2
System.out.println("Using Constructor 2 - ");
// Again creating object of class inside main()
// method
StringTokenizer st2 = new StringTokenizer(
"JAVA : Code : String", " :");
// If tokens are present
while (st2.hasMoreTokens())
// Print all tokens
System.out.println(st2.nextToken());
// Constructor 3
System.out.println("Using Constructor 3 - ");
// Again creating object of class inside main()
// method
StringTokenizer st3 = new StringTokenizer(
"JAVA : Code : String", " :", true);
while (st3.hasMoreTokens())
System.out.println(st3.nextToken());
}
}
OUTPUT
java -cp /tmp/TUVhwyKJYi/GFG
Using Constructor 1 -
Hello
Geeks
How
are
you
Using Constructor 2 -
JAVA
Code
String
Using Constructor 3 -
JAVA
Code
String
Example of Methods of StringTokenizer Class
// Java Program to implement
//
import java.util.*;
// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
// Creating a StringTokenizer
StringTokenizer str = new StringTokenizer(
"Welcome to GeeksforGeeks");
StringTokenizer temp = new StringTokenizer("");
// countTokens Method
int count = str.countTokens();
System.out.println(count);
// hasMoreTokens Methods
System.out.println("Welcome to GeeksforGeeks:
"+str.hasMoreTokens());
System.out.println("(Empty String) : "+temp.hasMoreTokens());
// nextElement() Method
System.out.println("\nTraversing the String:");
while(str.hasMoreTokens()){
System.out.println(str.nextElement());
}
}
}
OUTPUT
java -cp /tmp/kwcHxwTWe6/GFG
Welcome to GeeksforGeeks: true
(Empty String) : false
Traversing the String:
Welcome
to
GeeksforGeeks
Example of countTokens() method of the StringTokenizer class
This method calculates the number of tokens present in the tokenizer String.
import java.util.StringTokenizer;
public class StringTokenizer3
{
/* Driver Code */
public static void main(String args[])
{
/* StringTokenizer object */
StringTokenizer st = new StringTokenizer("Hello Everyone Have a nice day"," ");
/* Prints the number of tokens present in the String */
System.out.println("Total number of Tokens: "+st.countTokens());
}
Output
java -cp /tmp/PXORL5vnNg/StringTokenizer3
Total number of Tokens: 6
Write a program to accept a sentence which may be
terminated by either '.', '?' or '!' only. The words may be
separated by more than one blank space and are in UPPER
CASE.
Perform the following tasks:
1. Find the number of words beginning and ending with a vowel.
2. Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.
Test your program with the sample data and some random
data:
Example 1
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL
ANYMORE.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL
=3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO
QUARREL
Example 2
INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN
YOU ARE TODAY.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL
=2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW
THAN YOU TODAY
Example 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL
=0
LOOK BEFORE YOU LEAP
Example 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT
Ans:- import java.util.*;
public class VowelWord
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr = in.nextLine().trim().toUpperCase();
int len = ipStr.length();
char lastChar = ipStr.charAt(len - 1);
if (lastChar != '.'
&& lastChar != '?'
&& lastChar != '!') {
System.out.println("INVALID INPUT");
return;
String str = ipStr.substring(0, len - 1);
StringTokenizer st = new StringTokenizer(str);
StringBuffer sbVowel = new StringBuffer();
StringBuffer sb = new StringBuffer();
int c = 0;
while (st.hasMoreTokens()) {
String word = st.nextToken();
int wordLen = word.length();
if (isVowel(word.charAt(0))
&& isVowel(word.charAt(wordLen - 1))) {
c++;
sbVowel.append(word);
sbVowel.append(" ");
else {
sb.append(word);
sb.append(" ");
String newStr = sbVowel.toString() + sb.toString();
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A
VOWEL = " + c);
System.out.println(newStr);
public static boolean isVowel(char ch) {
ch = Character.toUpperCase(ch);
boolean ret = false;
if (ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
ret = true;
return ret;
Output
ENTER THE SENTENCE:
my name is anamika i ama
INVALID INPUT
ENTER THE SENTENCE:
anamika anaya asa is a student.
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 4
ANAMIKA ANAYA ASA A IS STUDENT