Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

First, write a reference class called XMLToken to represent a token object. It w

ID: 3844228 • Letter: F

Question

First, write a reference class called XMLToken to represent a token object. It will have only one instance variable, which is the token string itself. It will have the following constructor and instance methods: public XMLToken (String token): This creates an XMLToken object and initializes the instance variable with the stringparameter passed to it. public boolean isTag() This returns true iff the token is a tag, that is, it contains at least three character and the first and last characters are . public boolean isopeningTag() This returns true iff the token is an opening tag. public boolean isClosingTag() This returns true iff the token is a closing tag. public string getTagName: For a tag, this returns the tag name, that is, the alphanumeric text inside the tag. If the token is not a tag, this returns the empty string " ". Next, write a program called CountTokens that: Prompts the user for the pathname of a fle; Redirects StdIn to read from that file; Reads in the file's contents token by token (where tokens are separated by whitespace) and, for each token: creates a XMLToken object; adds the object to the end of an array list of XML tokens. Iterates throught the array list to count the number of words, opening tags, closing tags, and malformed tokens (that is, any token that is neither a word nor a tag). Prints the counts with explanatory text. You may test your program with the files found in the XML 1 test zip file. You can easily verify that your program produces the correct output by working out the counts of each test file by hand.

Explanation / Answer

The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.

An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:

A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.

A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

The following is one example of the use of the tokenizer. The code:

prints the following output:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens: