Page 331 - computer science (868) class 11
P. 331

Some of the methods of the StringTokenizer class are given in the following table.

                                           Methods                                Description
                              hasMoreTokens()                      It checks  for the availability  of more tokens  and
                                                                   returns true or false.
                              nextToken()                          It  returns the next token  as a String from the
                                                                   StringTokenizer object.
                              nextToken(String delim)              It returns the next token based on the delimiter.
                              countTokens()                        It returns the total number of tokens in the String.
                 Let us create some simple programs that will give you a better understanding of StringTokenizer class.


                  Program 1      To define a class named Words which will print words from a sentence having space, comma
                                 (,), question mark (?), and full stop (.) as delimiters. The class description is given as follows:

                                 Data Members
                                 string s, w        :   To store sentence and word respectively
                                 Member Methods
                                 void accept()      :   Accepts sentence
                                 void print()       :     Using  StringTokenizer  class to print words from the sentence
                                                        separated by spaces, comma, full stop or question mark
                                 static void main()   :   Creates objects and calls the other methods

                   1      import java.util.*;
                   2      class Words

                   3      {
                   4          String s, w;

                   5          void accept() // accepting sentence
                   6          {

                   7              Scanner sc=new Scanner(System.in);
                   8              System.out.println("Enter sentence");

                   9              s=sc.nextLine();
                  10          }
                  11          void print()

                  12          {

                  13              //Creating object of StringTokenizer class
                  14              // and defining  space , . ? as delimeters
                  15              StringTokenizer st=new StringTokenizer(s," ,.?");

                  16              System.out.println("The words are:");
                  17              while(st.hasMoreTokens())  //Checking for words

                  18              {
                  19                  w=st.nextToken();  //Extracting words



                                                                                                                       329
                                                                                                    Basic Input/Output   329
   326   327   328   329   330   331   332   333   334   335   336