Source of TokenTest.java


  1: // Fig. 29.18: TokenTest.java
  2: // StringTokenizer class.
  3: import java.util.Scanner;
  4: import java.util.StringTokenizer;
  5: 
  6: public class TokenTest 
  7: {
  8:    // execute application
  9:    public static void main( String args[] )
 10:    {
 11:       // get sentence
 12:       Scanner scanner = new Scanner( System.in );
 13:       System.out.println( "Enter a sentence and press Enter" );
 14:       String sentence = scanner.nextLine();
 15: 
 16:       // process user sentence
 17:       StringTokenizer tokens = new StringTokenizer( sentence );
 18:       System.out.printf( "Number of elements: %d\nThe tokens are:\n",
 19:          tokens.countTokens() );
 20: 
 21:       while ( tokens.hasMoreTokens() )
 22:          System.out.println( tokens.nextToken() );
 23:    } // end main
 24: } // end class TokenTest
 25: 
 26: /**************************************************************************
 27:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 28:  * Pearson Education, Inc. All Rights Reserved.                           *
 29:  *                                                                        *
 30:  * DISCLAIMER: The authors and publisher of this book have used their     *
 31:  * best efforts in preparing the book. These efforts include the          *
 32:  * development, research, and testing of the theories and programs        *
 33:  * to determine their effectiveness. The authors and publisher make       *
 34:  * no warranty of any kind, expressed or implied, with regard to these    *
 35:  * programs or to the documentation contained in these books. The authors *
 36:  * and publisher shall not be liable in any event for incidental or       *
 37:  * consequential damages in connection with, or arising out of, the       *
 38:  * furnishing, performance, or use of these programs.                     *
 39:  *************************************************************************/