Java: Count number of tokens using StringTokenizer

In previous article, we have discussed about StringTokenizer class with various delimiters for splitting a String

 

Next question is whether it is possible to count number of tokens after splitting a String ?

Yes, it is possible to count number of tokens after splitting string using countTokens() method of StringTokenizer class

 

Method Signature:

public int countTokens();

 

Note:

 

Let us move forward to discuss an example on how to count number of tokens using StringTonizer’s countTokens() method

1. Count number of tokens when space is used as delimiter

Syntax:

StringTokenizer st1 = new StringTokenizer(str);

 

CountTokensAfterSplittingUsingSpaceAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class CountTokensAfterSplittingUsingSpaceAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "Water Wind Earth Sky Fire";

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str);

		System.out.println("Number of tokens, after String split : " 
				+ st.countTokens());

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {

			System.out.println("\nToken value is : " + st.nextToken());
			System.out.println("After iteration, number of tokens reduced to : " 
					+ st.countTokens());
		}
	}
}

Output:

Number of tokens, after String split : 5

Token value is : Rain
After iteration, number of tokens reduced to : 4

Token value is : Wind
After iteration, number of tokens reduced to : 3

Token value is : Earth
After iteration, number of tokens reduced to : 2

Token value is : Fire
After iteration, number of tokens reduced to : 1

Token value is : Sky
After iteration, number of tokens reduced to : 0

 

2. Count number of tokens when comma(,) is used as delimiter

Syntax:

StringTokenizer st2 = new StringTokenizer(str, “,”);

 

CountTokensAfterSplittingUsingCommaAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class CountTokensAfterSplittingUsingCommaAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "Apple,Banana,Mango,Orange,";

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, ",");

		System.out.println("Number of tokens, after String split : " 
				+ st.countTokens());

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {

			System.out.println("\nToken value is : " + st.nextToken());
			System.out.println("After iteration, number of tokens reduced to : " 
					+ st.countTokens());
		}
	}
}

Output:

Number of tokens, after String split : 4

Token value is : Apple
After iteration, number of tokens reduced to : 3

Token value is : Banana
After iteration, number of tokens reduced to : 2

Token value is : Mango
After iteration, number of tokens reduced to : 1

Token value is : Orange
After iteration, number of tokens reduced to : 0

 

3. Count number of tokens when pipe(|) is used as delimiter

Syntax:

StringTokenizer st2 = new StringTokenizer(str, “|”);

 

CountTokensAfterSplittingUsingPipeAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class CountTokensAfterSplittingUsingPipeAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "Bob|25|Engineer";

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, "|");

		System.out.println("Number of tokens, after String split : " 
				+ st.countTokens());

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {

			System.out.println("\nToken value is : " + st.nextToken());
			System.out.println("After iteration, number of tokens reduced to : " 
					+ st.countTokens());
		}
	}
}

Output:

Number of tokens, after String split : 3

Token value is : Bob
After iteration, number of tokens reduced to : 2

Token value is : 25
After iteration, number of tokens reduced to : 1

Token value is : Engineer
After iteration, number of tokens reduced to : 0

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!