Java-CountTokens and HasMoreToken

countTokens

The countTokens() method determines the number of tokens left to be parsed and returns the result.

hasMoreToken

The hasMoreToken() method returns true if one or more tokens remain in the string and returns false if there are none.

Program

Program Source

import java.util.StringTokenizer;

public class Javaapp {

    public static void main(String[] args) {
        
        String studentdata = "student1 : 40;student2 : 50;student3 : 60;";
        StringTokenizer st = new StringTokenizer(studentdata,";");
        
        System.out.println("TotelTokens : "+st.countTokens());
        while(st.hasMoreTokens())
        {
            System.out.println(st.nextToken());
        }
    }
}

Leave a Comment