Monday, 15 December 2014

// The "StringTokenizerExample2A" class.  Page 366 (middle).
// (from the Carter book)

// Example 2A --> how to use the version # 3 constructor
//                method of the StringTokenizer class,
//                which returns delimiter characters as
//                tokens, if a "flag" is set to true.
//
import java.util.*;
//
// The following statement:
public class StringTokenizerExample2A
{
    public static void main (String[] args)
    {
        // version # 3 constructor method:

        StringTokenizer st1 = new StringTokenizer
            ("12*(345+6789)", "*/+-()", true);
        // will create a StringTokenizer object called "st1" using the
        // given String literal.  The delimiters are any characters in
        // the given String "*/+-()".  Because the value of the third
        // argument ("flag") is set to true, the delimiters are treated
        // like tokens.  The resulting tokens of "st1" are, as follows:
        // "12"
        // "*"
        // "("
        // "345"
        // "+"
        // "6789"
        // ")"
        // To produce the output just above, code this while loop -->
        // this will print all the characters in the given String,
        // including the delimiter characters:
        while (st1.hasMoreTokens ())
        {
            System.out.println (st1.nextToken ());

        } // end of while loop

    } // main method
} // StringTokenizerExample2A class

No comments:

Post a Comment