- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
StringTokenizer methods in Java
The StringTokenizer class allows an application to break a string into tokens. Following are the methods −
Sr.No | Method & Description |
---|---|
1 | int countTokens() This method calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. |
2 | boolean hasMoreElements() This method returns the same value as the hasMoreTokens method. |
3 | boolean hasMoreTokens() This method tests if there are more tokens available from this tokenizer's string. |
4 | Object nextElement() This method returns the same value as the nextToken method, except that its declared return value is Object rather than String. |
5 | String nextToken() This method returns the next token from this string tokenizer. |
6 | String nextToken(String delim) This method returns the next token in this string tokenizer's string. |
Let us see some of the examples of the StringTokenize class displaying the usage of its method −
Here, we are using the countTokens() method to calculate the number of times that this tokenizer's nextToken method can be called before it generates an exception −
Example
import java.util.*; public class Main { public static void main(String[] args) { // creating string tokenizer StringTokenizer st = new StringTokenizer("Welcome to my website!"); // counting tokens System.out.println("Total tokens : " + st.countTokens()); } }
Output
Total tokens : 4
Now, let us see another example wherein we are using the nextElement() method. The nextElement() method is used to return the same value as the nextToken method, except that its declared return value is Object rather than String −
Example
import java.util.*; public class Main { public static void main(String[] args) { // creating string tokenizer StringTokenizer st = new StringTokenizer("This is it!"); // moving to next element st.nextElement(); // checking next to next element System.out.println("Next element is : " + st.nextElement()); } }
Output
Next element is : is
- Related Articles
- Methods of StringTokenizer class in Java.
- StringTokenizer class in Java
- Constructors of StringTokenizer class in Java.
- Java StringTokenizer and String Split Example.
- How can we use the StringTokenizer class in Java?
- How to split a Java String into tokens with StringTokenizer?
- Methods in Java
- Static methods vs Instance methods in Java
- Trigonometric methods in Java
- Enum Methods in Java
- Default methods in Java
- Dictionary Methods in Java
- What are defender methods or virtual methods in Java?
- What are methods in Java?
- Overloading Varargs Methods in Java

Advertisements