Thursday 24 January 2019

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.
  • 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.

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.
Example 
  1. import java.util.*;
  2. public class NewClass
  3. {
  4. public static void main(String args[])
  5. {
  6. System.out.println("Using Constructor 1 - ");
  7. StringTokenizer st1 =
  8. new StringTokenizer("Hello folks How are you", " ");
  9. while (st1.hasMoreTokens())
  10. System.out.println(st1.nextToken());
  11. System.out.println("Using Constructor 2 - ");
  12. StringTokenizer st2 =
  13. new StringTokenizer("JAVA : Code : String", " :");
  14. while (st2.hasMoreTokens())
  15. System.out.println(st2.nextToken());
  16. System.out.println("Using Constructor 3 - ");
  17. StringTokenizer st3 =
  18. new StringTokenizer("JAVA : Code : String", " :", true);
  19. while (st3.hasMoreTokens())
  20. System.out.println(st3.nextToken());
  21. }
  22. }
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.
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
  1. 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
  1. 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
  1. public int countTokens()
Return: The number of tokens remaining in the string using the current delimiter set.
Example 
  1. import java.util.*;
  2. public class NewClass
  3. {
  4. public static void main(String args[])
  5. {
  6. String mydelim = " : ";
  7. String mystr = "JAVA : Code : String : Tokenizer : flair";
  8. StringTokenizer flair3 =
  9. new StringTokenizer(mystr, mydelim);
  10. int count = flair3.countTokens();
  11. System.out.println("Number of tokens : " + count + "\n");
  12. for (int i = 0; i <count; i++)
  13. System.out.println("token at [" + i + "] : "
  14. + flair3.nextToken());
  15. while (geeks3.hasMoreTokens())
  16. System.out.println(geeks3.nextToken());
  17. }
  18. }
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 
  1. 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 
  1. public boolean hasMoreElements()
Return: True if tokens are present in the string, else false.
Example 
  1. import java.util.*;
  2. public class NewClass
  3. {
  4. public static void main(String args[])
  5. {
  6. String mydelim = " : ";
  7. String mystr = "JAVA : Code : String : Tokenizer : Flair";
  8. StringTokenizer geeks =
  9. new StringTokenizer(mystr, mydelim);
  10. int count = geeks.countTokens();
  11. System.out.println("Number of tokens : " + count)
  12. while (flair.hasMoreElements())
  13. System.out.println(flair.nextElement());
  14. }
  15. }
Output
Number of tokens: 5
JAVA
Code
String
Tokenizer
Flair
So, this was all about StringTokenizer in Java. Hope you like our explanation.

No comments:

Post a Comment

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...