# JDC Tech Tips No. 11

JDCTechTips@sun.com
Sat, 27 Jun 1998 04:45:59 GMT

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: river99@gte.net: "Service Update"
- Previous message: huy_d1@verifone.com: "setting system class path"

    Date: Sat, 27 Jun 1998 04:45:59 GMT

    Message-Id: <199806270445.EAA04309@hermes.javasoft.com>

    From: JDCTechTips@sun.com

    Subject: JDC Tech Tips No. 11

    To: JDCTechTips@sun.com

-WELCOME- to the Java(sm) Developer Connection(sm) Tech Tips. This issue
covers string tokenizing and using the enumeration interface. The JDC
Team-

J D C T E C H T I P S
TIPS, TECHNIQUES, AND SAMPLE CODE
* String Tokenizing
* The Enumeration Interface

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
T I P S , T E C H N I Q U E S , A N D S A M P L E C O D E

STRING TOKENIZING. A very common programming problem is one where you have
a string of characters, that you'd like to split into individual chunks (or
tokens). Typically, white space (created by inserting spaces and tabs) is
used to separate one token from the next.

The Java language provides an easy way to solve this problem: the
java.util.StringTokenizer class. You create a new instance of
StringTokenizer, specifying a string to be tokenized. Then hasMoreTokens
and nextToken are called repeatedly to obtain the next token from the
string.

To see how tokenizing works, consider the following example:

import java.util.StringTokenizer;

public class token {

public static void main(String args[])
{
String testdata = "123,456, 789,,,1011 \n 1213";

StringTokenizer st =
new StringTokenizer(testdata, " ,\t\r\n");

while (st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println(token);
}
}

}

In the example, a string of numeric test data is set up, and a
StringTokenizer object is established to parse the string. By default, the
standard delimiters for tokenizing are " \t\r\n", and the example adds
comma to the standard list. hasMoreTokens is called to determine whether
the string of data has been exhausted, and if not, nextToken is called to
retrieve the next token. In this example tokens are numeric values that
represent fields of some type.

The program output is the following:

123
456
789
1011
1213

StringTokenizer has additional useful facilities. If you are interested in
learning more about them, one method to try is countTokens, a method that
returns a count of the number of tokens in the specified string.

THE ENUMERATION INTERFACE. In the string tokenizing example above, a
string is tokenized, and the tokens are retrieved and printed in turn. The
example above illustrates a general concept: enumeration. That is, all
the values in a list of some type are returned one at a time until the list
is exhausted. In the above example the list is a list of tokens extracted
from a string.

The Java utility library provides an interface to capture this type of
behavior. java.util.Enumeration declares two methods: hasMoreElements and
nextElement. The first of these returns a true/false value, according to
whether a given enumeration is exhausted. The second method returns the
next element as an Object reference.

Enumeration is an interface, meaning that it simply describes methods that
must be present in any class that implements the interface. The
description doesn't say anything about how those methods are implemented.
To make the idea of enumerations more concrete, here's a specific example
that defines a Stepper class to step through the values in an array:

import java.util.Enumeration;

class Stepper implements Enumeration {
private Object vec[];
private int currpos;
public Stepper(Object x[])
{
vec = x;
currpos = 0;
}
public boolean hasMoreElements()
{
return currpos < vec.length;
}
public Object nextElement()
{
return vec[currpos++];
}
}
public class enum {
public static void main(String args[])
{
String data[] = {"alpha", "beta", "gamma", "delta"};
Stepper st = new Stepper(data);
while (st.hasMoreElements())
System.out.println(st.nextElement());
}
}

Stepper implements the Enumeration interface, so it must define the methods
hasMoreElements and nextElement. The implementation simply defines an
internal variable "currpos" that defines the current position in the array.
If this value is less than the array's length, there are more elements, and
the next element is retrieved based on the current array position, which is
then incremented.

Stepper will work with any array of objects. String is a subclass of
Object, and as such, a String[] array reference can be assigned to an
Object[] array reference (via a "widening" conversion).

Finally, the Enumeration interface is particularly useful in cases where
you want to hide the details of the enumerated data structure. For
example, a common error might be to assume that the indices of an array
range in value from 1 to the array length, when, in fact, the range is from
0 to the length - 1. Using an enumeration hides this detail, and is less
error-prone than explicitly indexing through the array.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-- NOTE --

The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only. To remove your name from the list, see
SUBSCRIBE/UNSUBSCRIBE below.

-- FEEDBACK --

Comments? Send your feedback on the JDC Tech Tips to:

JDCTechTips@Sun.com

-- SUBSCRIBE/UNSUBSCRIBE --

This mailing is sent to you because you elected to receive JDC Email when
you registered as a JDC member. To unsubscribe from JDC Email, go to the
following address and enter the email address you wish to remove from the
mailing list:

http://developer.java.sun.com/unsubscribe.html

-- ARCHIVES --

You'll find the JDC Tech Tips archives at:

http://developer.javasoft.com/developer/javaInDepth/TechTips/index.html

-- COPYRIGHT --

Copyright 1998 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://developer.javasoft.com/developer/copyright.html

The JDC Tech Tips are written by Glen McCluskey.

JDC Tech Tips No. 11
June 23, 1998

- Next message: river99@gte.net: "Service Update"
- Previous message: huy_d1@verifone.com: "setting system class path"