Java Reference
In-Depth Information
// otherwise it's text, so format it.
StringTokenizer st
=
new
new
StringTokenizer
(
line
);
while
while
(
st
.
hasMoreTokens
()) {
String word
=
st
.
nextToken
();
// If this word would go past the margin,
// first dump out anything previous.
iif
(
outBuf
.
length
() +
word
.
length
() >
COLWIDTH
) {
out
.
println
(
outBuf
);
outBuf
.
setLength
(
0
);
}
outBuf
.
append
(
word
).
append
(
' '
);
}
}
}
iif
(
outBuf
.
length
() >
0
) {
out
.
println
(
outBuf
);
}
else
else
{
out
.
println
();
}
}
}
A slightly fancier version of this program,
Fmt2
, is in the online source for this topic. It uses
“dot commands”—lines beginning with periods—to give limited control over the formatting.
A family of “dot command” formatters includes Unix's
roff
,
nroff, troff
, and
groff
, which are
in the same family with programs called
runoff
on Digital Equipment systems. The original
for this is J. Saltzer's
runoff
, which first appeared on Multics and from there made its way in-
to various OSes. To save trees, I did not include
Fmt2
here; it subclasses
Fmt
and overrides
the
format()
method to include additional functionality (the source code is in the full
javas-
rc
repository for the topic).
Program: Soundex Name Comparisons
The difficulties in comparing (American-style) names inspired the U.S. Census Bureau to de-
velop the Soundex algorithm in the early 1900s. Each of a given set of consonants maps to a
particular number, the effect being to map similar-sounding names together, on the grounds
that in those days many people were illiterate and could not spell their family names consist-
ently. But it is still useful today—for example, in a company-wide telephone book applica-
tion. The names Darwin and Derwin, for example, map to D650, and Darwent maps to D653,