// (from the Carter book)
//
// INTRODUCTION - PART A
//
// StringTokenizer Methods --> how they are defined in their
// headers and how they are used.
//
// We have already seen 2 of these methods in Examples 1A and 1B:
// "hasMoreTokens" --> is true only if there are more tokens available, and
// "nextToken" --> is the next token in the StringTokenizer object.
//
// Here, we will take a closer look at these 2 methods, plus one other
// method called "countTokens".
// Collectively, these 3 methods allow us to scan through any given
// String, from left to right, one token at a time, extracting tokens
// as we proceed.
import java.util.*;
//
// The following are the header definitions for the 3 StringTokenizer
// methods:
public class StringTokenizerIntroPartA
{
public static void main (String[] args)
{
// 1. Here is the header definition of the "nextToken" method:
public String nextToken ()
//
// This returns the next token from the implicit StringTokenizer
// object. The first time that this method is called, it returns
// the leftmost token in the String. Each call causes the method
// to move (right) along the String to the position of the next token. If
// there are no more tokens, the method "throws" a
//"NoSuchElementException", meaning that a token no longer exists.
//
// 2. Here is the header definition of the "hasMoreTokens" method:
public boolean hasMoreTokens ()
//
// This method checks to see if there are any more tokens available
// from the implicit StringTokenizer object. The method returns
// true, if and only if, there is at least one token in the String
// after the current position.
//
// 3. Here is the header definition of the "countTokens" method:
public int countTokens ()
//
// This method returns the number of tokens remaining in the implicit
// StringTokenizer object.
} // main method
} // StringTokenizerIntroPartA class
No comments:
Post a Comment