// StringTokenizerDemo.java // Example of using the StringTokenizer // APPLET CODE="StringTokenizerDemo" HEIGHT=400 WIDTH=600 import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class StringTokenizerDemo extends JApplet implements ActionListener { // GUI components Container c; JLabel prompt; JTextField input; JTextArea output; public void init( ) { c = getContentPane( ); c.setLayout( new FlowLayout( ) ); prompt = new JLabel( "Enter a sentence and hit Enter" ); c.add( prompt ); input = new JTextField( 50 ); input.addActionListener( this ); c.add( input ); output = new JTextArea( 10, 40 ); output.setEditable( false ); c.add( output ); } public void actionPerformed( ActionEvent e ) { String givenString = input.getText( ); StringTokenizer tokens = new StringTokenizer( givenString ); output.setText( "" ); // clears the text area output.append( "Number of tokens: " + tokens.countTokens( ) + "\nThe tokens are:\n" ); while( tokens.hasMoreTokens( ) ) output.append( tokens.nextToken( ) + "\n" ); } }