The hasMoreTokens
method of the StringTokenizer
class is an instance method that checks if there are any more tokens in the tokenizer’s string. If the result of this procedure is true
, then there are more tokens to be retrieved.
The next token can be retrieved via the
nextToken()
method, which makes the pointer move forward in the list of tokens after the tokenization of the string.
The StringTokenizer
class is defined in the java.util
package.
To import the StringTokenizer
class, use the import statement below:
import java.util.StringTokenizer;
public boolean hasMoreTokens()
This method has no parameters.
hasMoreTokens()
returns true
if there is at least one token following the current place in the string. Otherwise, the method returns false
.
In the code below:
We create an object of the StringTokenizer
class and pass the s
string and the -
delimiter.
We first use the hasMoreTokens()
method to check if any tokens are present in stringTokenizer
. Since stringTokenizer
has tokens, the method returns true
.
We then loop through all the available tokens using the hasMoreTokens()
and nextToken()
methods.
After all the tokens are retrieved, the hasMoreTokens()
method returns false
, as there are no more tokens available.
import java.util.StringTokenizer;public class Main{public static void main(String[] args){String s = "hello-hi-educative";StringTokenizer stringTokenizer = new StringTokenizer(s, "-");System.out.println(stringTokenizer.hasMoreTokens());while(stringTokenizer.hasMoreTokens()){System.out.println(stringTokenizer.nextToken());}System.out.println("End of all tokens");System.out.println(stringTokenizer.hasMoreTokens());}}