📄 tij0113.html
字号:
which get the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Counter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for a particular
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>sortedKeys( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which produces an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Enumeration</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
finish the change in the shape of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SortedWordCount</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’s
interface.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you can see the use of a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SortedWordCount</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to open and count the words in a file – it just takes two lines of code.
Then an enumeration to a sorted list of keys (words) is extracted, and this is
used to pull out each key and associated
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Count</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Note that the call to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>cleanup( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is necessary to ensure that the file is closed.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
second example using
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StreamTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can be found in Chapter 17.
</FONT><a name="_Toc375545399"></a><a name="_Toc408018627"></a><P></DIV>
<A NAME="Heading332"></A><H3 ALIGN=LEFT>
StringTokenizer</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Although
it isn’t part of the IO library, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
has sufficiently similar functionality to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StreamTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that it will be described here.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index1178"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns the tokens within a string one at a time. These tokens are consecutive
characters delimited by tabs, spaces, and newlines. Thus, the tokens of the
string “Where is my cat?” are “Where”,
“is”, “my”, and “cat?” Like the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StreamTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">you
can tell the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to break up the input in any way that you want, but with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">you
do this by passing a second argument to the constructor, which is a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">of
the delimiters you wish to use. In general, if you need more sophistication,
use a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StreamTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
ask a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object for the next token in the string using the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>nextToken( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, which either returns the token or an empty string to indicate that no
tokens remain.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
an example, the following program performs a limited analysis of a sentence,
looking for key phrase sequences to indicate whether happiness or sadness is
implied.
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: AnalyzeSentence.java</font>
<font color="#009900">// Look for particular sequences</font>
<font color="#009900">// within sentences.</font>
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> AnalyzeSentence {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
analyze("I am happy about <font color="#0000ff">this</font>");
analyze("I am not happy about <font color="#0000ff">this</font>");
analyze("I am not! I am happy");
analyze("I am sad about <font color="#0000ff">this</font>");
analyze("I am not sad about <font color="#0000ff">this</font>");
analyze("I am not! I am sad");
analyze("Are you happy about <font color="#0000ff">this</font>?");
analyze("Are you sad about <font color="#0000ff">this</font>?");
analyze("It's you! I am happy");
analyze("It's you! I am sad");
}
<font color="#0000ff">static</font> StringTokenizer st;
<font color="#0000ff">static</font> <font color="#0000ff">void</font> analyze(String s) {
prt("\nnew sentence >> " + s);
<font color="#0000ff">boolean</font> sad = <font color="#0000ff">false</font>;
st = <font color="#0000ff">new</font> StringTokenizer(s);
<font color="#0000ff">while</font> (st.hasMoreTokens()) {
String token = next();
<font color="#009900">// Look until you find one of the</font>
<font color="#009900">// two starting tokens:</font>
<font color="#0000ff">if</font>(!token.equals("I") &&
!token.equals("Are"))
<font color="#0000ff">continue</font>; <font color="#009900">// Top of while loop</font>
<font color="#0000ff">if</font>(token.equals("I")) {
String tk2 = next();
<font color="#0000ff">if</font>(!tk2.equals("am")) <font color="#009900">// Must be after I</font>
<font color="#0000ff">break</font>; <font color="#009900">// Out of while loop</font>
<font color="#0000ff">else</font> {
String tk3 = next();
<font color="#0000ff">if</font>(tk3.equals("sad")) {
sad = <font color="#0000ff">true</font>;
<font color="#0000ff">break</font>; <font color="#009900">// Out of while loop</font>
}
<font color="#0000ff">if</font> (tk3.equals("not")) {
String tk4 = next();
<font color="#0000ff">if</font>(tk4.equals("sad"))
<font color="#0000ff">break</font>; <font color="#009900">// Leave sad false</font>
<font color="#0000ff">if</font>(tk4.equals("happy")) {
sad = <font color="#0000ff">true</font>;
<font color="#0000ff">break</font>;
}
}
}
}
<font color="#0000ff">if</font>(token.equals("Are")) {
String tk2 = next();
<font color="#0000ff">if</font>(!tk2.equals("you"))
<font color="#0000ff">break</font>; <font color="#009900">// Must be after Are</font>
String tk3 = next();
<font color="#0000ff">if</font>(tk3.equals("sad"))
sad = <font color="#0000ff">true</font>;
<font color="#0000ff">break</font>; <font color="#009900">// Out of while loop</font>
}
}
<font color="#0000ff">if</font>(sad) prt("Sad detected");
}
<font color="#0000ff">static</font> String next() {
<font color="#0000ff">if</font>(st.hasMoreTokens()) {
String s = st.nextToken();
prt(s);
<font color="#0000ff">return</font> s;
}
<font color="#0000ff">else</font>
<font color="#0000ff">return</font> "";
}
<font color="#0000ff">static</font> <font color="#0000ff">void</font> prt(String s) {
System.out.println(s);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
each string being analyzed, a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>while</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
loop is entered and tokens are pulled off the string. Notice the first
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>if</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement, which says to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>continue</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(go back to the beginning of the loop and start again) if the token is neither
an “I” nor an “Are.”
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
means that it will get tokens until an “I” or an “Are”
is found. You might think to use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
instead of the <A NAME="Index1179"></A><A NAME="Index1180"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, but that won’t work correctly, since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>==</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
compares handle values while
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>equals( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
compares contents.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
logic of the rest of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>analyze( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is that the pattern that’s being searched for is “I am
sad,” “I am not happy,” or “Are you sad?” Without
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>break</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statement, the code for this would be even messier than it is. You should be
aware that a typical parser (this is a primitive example of one) normally has a
table of these tokens and a piece of code that moves through the states in the
table as new tokens are read.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
should think of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
only as shorthand for a simple and specific kind of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StreamTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, if you have a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that you want to tokenize and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is too limited, all you have to do is turn it into a stream with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StringBufferInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and then use that to create a much more powerful
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>StreamTokenizer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc408018628"></a><a name="_Toc375545400"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0112.html">Prev</a> | <a href="tij0114.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -