StringTokenizer In Java | StringTokenizer Constructors

1. Objective

In our previous tutorial, we discussed the Java StringBuffer. Here, we are going to study StringTokenizer in Java Program and how Java Tokenizes String with the help of String Constructor and StringTokenizer Methods. Moreover, we are going to see StringTokenizer in Java with delimiter and also multiple delimiters in detail.

So, let’s start Java StringTokenizer tutorial.

StringTokenizer In Java

StringTokenizer In Java | StringTokenizer Constructors

2. What is StringTokenizer in Java?

  • Basically, StringTokenizer Class is used for creating tokens in Java.
  • It allows an application to break or split into small parts.
  • Each Split String part is called Token.
  • A StringTokennizer in Java, object keeps the string in the present position as it is to be tokenized.
  • By taking a substring of the string a token can return that utilize to make the StringTokenizer protest.

Do you know Why We Use Assertion in Java?

3. Java StringTokenizer

In this Java StringTokenizer, we will see StringTokenizer Constructors and Methods in StringTokenizer. So, let’s discuss both of them in detail:

a. StringTokenizer Constructors

  • StringTokenizer(String str) :

str is a string to be tokenized and it considers default delimiters like newline, space, tab, carriage return and form feed which can be further tokenized.

  • StringTokenizer(String str, String delim) :

delim is set of delimiters that are used to tokenize the given string.

  • StringTokenizer(String str, String delim, boolean flag):

Since the first two parameters have the same meaning. The flag serves following purpose. If the flag is false, delimiter characters serve to separate tokens and if the flag is true, delimiter characters are considered to be tokens.

Literals in Java – Integral, Floating, Char, String, Boolean

Example 

import java.util.*;
public class NewClass
{
   public static void main(String args[])
   {
       System.out.println("Using Constructor 1 - ");
       StringTokenizer st1 =
            new StringTokenizer("Hello folks How are you", " ");
       while (st1.hasMoreTokens())
           System.out.println(st1.nextToken());
       System.out.println("Using Constructor 2 - ");
       StringTokenizer st2 =
            new StringTokenizer("JAVA : Code : String", " :");
       while (st2.hasMoreTokens())
           System.out.println(st2.nextToken());
       System.out.println("Using Constructor 3 - ");
       StringTokenizer st3 =
            new StringTokenizer("JAVA : Code : String", " :",  true);
       while (st3.hasMoreTokens())
           System.out.println(st3.nextToken());
   }
}

Output 
Using Constructor 1 –
Hello
folks
How
are
you
Using Constructor 2 –
JAVA
Code
String
Using Constructor 3 –
JAVA
:
Code
:
String

b. Methods of Java StringTokenizer

Some of the foremost used ways for StringTokenization in Java are:

i. hasMoreTokens()

The method java.util.StringTokenizer.hasMoreTokens() plays role in testing, if tokens are present for the StringTokenizer’s string.

Read about – Java Character Class Methods

Basically, 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()

Returns: True if and only if next token to the current position in the string exists, else false.

ii. nextToken()

So, the method java.util.StringTokenizer.nextToken() returns next token from the given StringTokenizer.
Syntax

public String nextToken()
  • Return: The next token from the given StringTokenizer if present.
  • Throws: NoSuchElementException – if no more token are left.

iii. countTokens()

This method java.util.StringTokenizer.countTokens() returns  total number of tokens which are present.

Hence, the number further use nextToken() method before it gives an exception and use it.

Syntax

public int countTokens()

Return: The number of tokens remaining in the string using the current delimiter set.
Example 

import java.util.*;
public class NewClass
{
   public static void main(String args[])
   {
       String mydelim = " : ";
       String mystr = "JAVA : Code : String : Tokenizer : flair";
       StringTokenizer flair3 =
                         new StringTokenizer(mystr, mydelim);
       int count = flair3.countTokens();
       System.out.println("Number of tokens : " + count + "\n");
       for (int i = 0; i <count; i++)
           System.out.println("token at [" + i + "] : "
                              + flair3.nextToken());
       while (geeks3.hasMoreTokens())
           System.out.println(geeks3.nextToken());
   }
}

Follow this link to know about Lambda Expression in Java

Output

Number of tokens: 5

token at [0]: JAVA

token at [1]: Code

token at [2]: String

token at [3]: Tokenizer

token at [4]: Flair

iv. nextElement()

So, the method returns Object rather than String and java.util.StringTokenizer.nextElements() works similar to nextToken exists so that this class can implement the Enumeration interface.

Syntax 

public Object nextElement()

Return: The next token from the given StringTokenizer.
Throws: NoSuchElementException – if there are no more tokens left.

v. hasMoreElements()

Next, in this method java.util.StringTokenizer.hasMoreElements() returns same value as hasMoreToken.

Syntax 

public boolean hasMoreElements()

Return: True if tokens are present in the string, else false.

Example 

import java.util.*;
public class NewClass
{
   public static void main(String args[])
   {
       String mydelim = " : ";
       String mystr = "JAVA : Code : String : Tokenizer : Flair";
       StringTokenizer geeks =
                     new StringTokenizer(mystr, mydelim);
       int count = geeks.countTokens();
       System.out.println("Number of tokens : " + count)
       while (flair.hasMoreElements())
           System.out.println(flair.nextElement());
   }
}

Let’s Learn Java Number Methods

Output

Number of tokens: 5
JAVA
Code
String
Tokenizer
Flair

So, this was all about StringTokenizer in Java. Hope you like our explanation.

4. Conclusion

Hence, in this Java StringTokenizer tutorial, we have learned a very important class in Java and that is StringTokenizer Class. Moreover, we saw StringTokenizer Constructors and its types. In addition, we discussed StringTokenizer in Java examples with code and output. Finally, we learned methods of StringTokenizer in Java. At last, we saw StringTokenizer in Java with delimiter and multiple delimiters and also nexttoken in Java. Furthermore, if you have any query, feel free to ask in the comment section.

See also – reflection in Java 

For reference  

Leave a Reply

Your email address will not be published. Required fields are marked *