Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

StringTokenizer in Java

  • You can split a String into tokens by using the java.util.StringTokenizer class. It is an easy method for breaking a string. It is a Java legacy class.

  • Unlike the StreamTokenizer class, the String Tokenizer does not offer the ability to distinguish between numbers, quoted strings, identifiers, etc.

  • The delimiters can be given to the tokens individually or all at once when using the StringTokenizer class.





Important Constructors of StringBuffer Class


  • StringTokenizer(String s) : With the specified string, it creates a StringTokenizer.

  • StringTokenizer(String s , String delim) : The "StringTokenizer" class is use to create tokens from a given string, based on the provided delimiter.

  • StringTokenizer(String s, String delim, boolean returnValue) : The given input, consisting of a string, delimiter, and returnValue, is utilized to construct a StringTokenizer. Delimiter characters are considered as tokens when the returnValue is true. If the condition is false, the delimiter characters are used to divide the tokens.






StringTokenizer

Important methods of StringBuffer class

Important methods of StringBuffer class are mention below


Method Description
hasMoreTokens() It checks to see if there are any additional tokens available.
String nextToken(String delim) Based on the delimiter, it returns the next token.
String nextToken() It returns the StringTokenizer object's next token.
Object nextElement() It is similar to nextToken(), but the return type is Object.
int countTokens() The total number of tokens is returned.


StringTokenizer Class example


Java StringTokenizer hasMoreTokens() Methods


StringTokenizer class on basis of whitespace.


 // Java example of String length() Methods
import java.util.StringTokenizer;
public class Main {	
    public static void main(String args[]){  		 
    StringTokenizer st = new StringTokenizer("I Love India"," ");  
     while (st.hasMoreTokens()) {  
         System.out.println(st.nextToken());  
     }  
   } 
}



Output:

I 
Love 
India 


Java StringTokenizer hasMoreElements() Method


The hasMoreTokens() function of the StringTokenizer class produces identical results to the hasMoreElements function. The only difference lies in the fact that the Enumeration interface can be utilized for implementation.


 // Java example of StringTokenizer hasMoreElements() Method 
import java.util.StringTokenizer;
public class Main {	
    public static void main(String args[]){  		 
    StringTokenizer st = new StringTokenizer("I Love India"," ");    
     while (st.hasMoreElements()){    
         System.out.println(st.nextToken());    
     }      
   } 
}



Output:

I 
Love 
India 

Java StringTokenizer countTokens() Method


The tokenizer String's total number of tokens is determined by the countTokens method.


 // Java example of StringTokenizer  countTokens()  Method 
import java.util.StringTokenizer;
public class Main {	
    public static void main(String args[]){  		 
       StringTokenizer st = new StringTokenizer("I Love India"," ");    
       System.out.println("Total Tokens: "+st.countTokens());        
   } 
}



Output:

Total Tokens: 3 

Java StringTokenizer nextElement() Method


The next token object in the tokenizer String is returned by nextElement(). It can use the interface for enumeration.


 // Java example of StringTokenizer nextElement  Method 
import java.util.StringTokenizer;
public class Main {	
    public static void main(String args[]){  		 
    StringTokenizer st = new StringTokenizer("I Love India"," ");     
     while (st.hasMoreTokens()){    // it check for more tokens         
    	System.out.println(st.nextElement());    
     }     
   } 
}



Output:

I 
Love 
India 

Java Exception Handling Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.