StringTokenizer In Java

The StringTokenizer class in Java is a legacy class that tokenizes (split) strings into individual tokens based on a specified delimiter. It’s part of the java.util package is considered somewhat outdated, as more modern alternatives like String.split() and regular expressions are often preferred for string manipulation tasks.

Analyzing its functionality, StringTokenizer provides a straightforward and efficient way to process textual data. It is particularly useful when dealing with structured text data, like CSV files, where you must extract individual data elements.

StringTokenizer In Java Use Case

There are several reasons why you might use the StringTokenizer class in Java:

Legacy Compatibility: One of the primary reasons for using StringTokenizer is legacy code compatibility. It has been available since the early versions of Java, and some older codebases may still rely on it. In such cases, it’s used to maintain compatibility with existing code.

Simple String Tokenization: StringTokenizer provides a straightforward way to split strings into tokens using a single character delimiter or a fixed set of delimiters. It’s particularly useful when you have a string with a predictable and simple structure and want to quickly break it down into its constituent parts.

Efficiency: StringTokenizer can be more efficient than regular expressions or more complex string manipulation methods for basic tokenization tasks with simple delimiters. It’s designed for speed and simplicity, making it suitable for scenarios where performance is a concern.

Minimizing Memory Usage: StringTokenizer is memory-efficient as it does not create an array of substrings like String.split(). Instead, it iterates through the original string and returns tokens simultaneously. This can be beneficial when working with large strings where memory usage is a concern.

Compatibility with Java 1.0: If you’re dealing with very old Java code or need to support legacy systems, StringTokenizer is a choice because it’s been available since Java 1.0. This means it can be used in environments where more modern features of Java may not be available.

Constructor of StringTokenizer Class

The StringTokenizer class in Java has two constructors that you can use to create instances of the class. These constructors allow you to specify how you want to tokenize (split) a given string based on a delimiter or set of delimiters.

Here’s an explanation of the constructors:

StringTokenizer(String str)

This constructor creates a StringTokenizer object with the given input string str as the source for tokenization. By default, this constructor uses whitespace characters (spaces, tabs, and newlines) as delimiters. It treats consecutive delimiter characters as a single delimiter.

Example usage:

String input = "Hello World Java";
StringTokenizer tokenizer = new StringTokenizer(input);

In this example, the StringTokenizer object tokenizer will tokenize the input string “Hello World Java” into three tokens: “Hello,” “World,” and “Java.”

StringTokenizer(String str, String delim)

This constructor creates a StringTokenizer object with two arguments: the input string str and a string delim representing the delimiter(s) to use for tokenization. It allows you to specify a custom delimiter or a set of delimiters as a string.

Example usage:

String input = "apple,orange,banana";
StringTokenizer tokenizer = new StringTokenizer(input, ",");

In this example, the StringTokenizer object tokenizer will tokenize the input string “apple, orange, banana” using the comma (“,”) as the delimiter. It will result in three tokens: “apple,” “orange,” and “banana.”

StringTokenizer(String str, String delim, boolean flag)

The StringTokenizer(String str, String delim, boolean flag) constructor in Java is used to create an instance of the StringTokenizer class, which is a utility class for breaking a given input string (str) into tokens using a specified delimiter (delim).

The additional flag parameter determines whether the delimiter characters are treated as tokens or used for splitting the input string. When the flag is set to true, the delimiter characters are treated as tokens, while when it’s set to false, they are ignored during tokenization.

For example, consider the input string “apple, banana, cherry, dates” and the following code snippet:

import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "apple,banana,cherry,dates";

		// Create a StringTokenizer with ',' as delimiter and 'true' flag
		StringTokenizer tokenizer = new StringTokenizer(input, ",", true);

		// Iterate through the tokens
		while (tokenizer.hasMoreTokens()) 
		{
			String token = tokenizer.nextToken();

			if (token.equals(",")) 
			{
				System.out.println("Found a comma as a token.");
			} 
			else 
			{
				System.out.println("Token: " + token);
			}
		}
	}
}

Output:

When the condition is “true

Token: apple
Found a comma as a token.
Token: banana
Found a comma as a token.
Token: cherry
Found a comma as a token.
Token: dates

The output when the condition is “false

Token: apple
Token: banana
Token: cherry
Token: dates

String tokenizer Java Methods

Once you have created a StringTokenizer object, you can use the following methods to work with the tokens:

int countTokens()An alias for nextToken() is provided for compatibility with legacy code.
boolean hasMoreTokens()Check if there are more tokens available in the string.
boolean hasMoreElements()An alias for hasMoreTokens() is provided for compatibility with legacy code.
String nextToken()Returns the next token from the string.
Object nextElement()An alias for nextToken() is provided for compatibility with legacy code.
String nextToken(String delim)Returns the next token using the specified delimiter delim. This allows you to change the delimiter temporarily for a single token retrieval.

Let’s explore the various methods of the StringTokenizer class in Java:

countTokens()

  • This method returns the number of tokens remaining in the tokenizer.
  • It can be used to determine how many tokens are left to be processed before reaching the end of the input string.
import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "Java is fun"; 
		StringTokenizer tokenizer = new StringTokenizer(input); 
		int tokenCount = tokenizer.countTokens(); 
		System.out.println("Number of tokens: " + tokenCount);
	}
}

Output:

Number of tokens: 3

hasMoreTokens()

This method returns a boolean value (true or false) indicating whether more tokens can be extracted from the input string.
It is often used in a loop to iterate through all the tokens in the input string.

import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "Welcome To SoftwareTestingO"; 
		StringTokenizer tokenizer = new StringTokenizer(input); 
		while (tokenizer.hasMoreTokens()) 
		{
			System.out.println("Has more tokens: " + tokenizer.hasMoreTokens());
			// Consume the token
			tokenizer.nextToken(); 
		}
	}
}

Output:

Has more tokens: true
Has more tokens: true
Has more tokens: true

nextElement()

  • This method is similar to nextToken() but returns the next token as an Object instead of a String.
  • It is provided for compatibility with older Java APIs that used Enumeration.
import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "Welcome To SoftwareTestingO"; 
		StringTokenizer tokenizer = new StringTokenizer(input); 
		while (tokenizer.hasMoreElements()) 
		{
			Object element = tokenizer.nextElement(); 
			System.out.println("Element: " + element);
		}
	}
}

Output:

Element: Welcome
Element: To
Element: SoftwareTestingO

hasMoreElements()

  • This method is analogous to hasMoreTokens() but returns a boolean value indicating whether more elements (tokens) can be extracted.
  • Like nextElement(), it is also for compatibility with older APIs.
import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "Welcome To SoftwareTestingO"; 
		StringTokenizer tokenizer = new StringTokenizer(input); 
		while (tokenizer.hasMoreElements()) 
		{
			System.out.println("Has more elements: " + tokenizer.hasMoreElements()); 
			// Consume the element
			tokenizer.nextElement(); 
		}
	}
}

Output:

import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "Welcome To SoftwareTestingO"; 
		StringTokenizer tokenizer = new StringTokenizer(input); 
		while (tokenizer.hasMoreElements()) 
		{
			System.out.println("Has more elements: " + tokenizer.hasMoreElements()); 
			// Consume the element
			tokenizer.nextElement(); 
		}
	}
}

Output:

Has more elements: true
Has more elements: true
Has more elements: true

nextToken()

  • This method retrieves and returns the next token from the input string.
  • It advances the tokenizer’s position to the next token.
  • If there are no more tokens left, it throws a NoSuchElementException.
  • You typically use this method in a loop to process each token sequentially.
import java.util.StringTokenizer;
public class StringTokenizerExample 
{
	public static void main(String[] args)
	{
		String input = "Welcome To SoftwareTestingO"; 
		StringTokenizer tokenizer = new StringTokenizer(input); 
		while (tokenizer.hasMoreTokens()) 
		{
			String token = tokenizer.nextToken(); 
			System.out.println("Token: " + token); 
		}
	}
}

Outlook:

Token: Welcome
Token: To
Token: SoftwareTestingO

Here’s an example of how you might use these methods:

import java.util.StringTokenizer;
public class StringTokenizerMethodsExample 
{
	public static void main(String[] args)
	{
		String input = "apple,banana,cherry,dates";

		// Create a StringTokenizer with ',' as delimiter
		StringTokenizer tokenizer = new StringTokenizer(input, ",");

		// Using the methods
		System.out.println("Total tokens: " + tokenizer.countTokens());

		while (tokenizer.hasMoreTokens())
		{
			String token = tokenizer.nextToken();
			System.out.println("Token: " + token);
		}

		// Using nextElement() and hasMoreElements()
		tokenizer = new StringTokenizer(input, ",");
		while (tokenizer.hasMoreElements()) 
		{
			Object token = tokenizer.nextElement();
			System.out.println("Element: " + token);
		}
	}
}

Output:

Token: dates
Element: apple
Element: banana
Element: cherry
Element: dates

These methods provide the essential functionality for tokenizing strings and extracting tokens in various ways, making it easier to work with structured textual data. You can choose the appropriate method for your tokenization needs depending on your requirements.

Note: While StringTokenizer is a convenient choice for simple tokenization tasks, if you need more advanced string parsing capabilities, you may consider using regular expressions or the split() method provided by the String class.

However, it’s important to note that while StringTokenizer has its uses, it also has limitations:

  • It only allows for simple character-based delimiters, making it less versatile than regular expressions for complex tokenization patterns.
  • It returns tokens as strings, which may require additional parsing if you work with non-string data types.
  • It lacks the flexibility and power of regular expressions for more advanced string manipulation tasks.

Conclusion:

While StringTokenizer remains a valuable tool for specific string parsing tasks, it’s essential for Java developers to be aware of alternative methods like split() and regular expressions.

If you have any doubts or questions regarding the “StringTokenizer Class in Java,” please feel free to comment below. Your queries are essential; we’re here to provide clarity and assistance.

Additionally, if you have any suggestions on how we can improve this article or if you’d like to see more in-depth coverage of related topics, we encourage you to share your feedback in the comment section. Your input helps us enhance our content to serve your needs better. Thank you for engaging with our articles!

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment