Java Reference
In-Depth Information
String s
=
"Father Charles Goes Down And Ends Battle"
;
// Put it in the stack frontwards
Stack
<
String
>
myStack
=
new
new
Stack
<>();
StringTokenizer st
=
new
new
StringTokenizer
(
s
);
while
while
(
st
.
hasMoreTokens
()) {
myStack
.
push
(
st
.
nextToken
());
}
// Print the stack backwards
System
.
out
.
print
(
'"'
+
s
+
'"'
+
" backwards by word is:\n\t\""
);
while
while
(!
myStack
.
empty
()) {
System
.
out
.
print
(
myStack
.
pop
());
System
.
out
.
print
(
' '
);
}
System
.
out
.
println
(
'"'
);
Expanding and Compressing Tabs
Problem
You need to convert space characters to tab characters in a file, or vice versa. You might
want to replace spaces with tabs to save space on disk, or go the other way to deal with a
device or program that can't handle tabs.
Solution
Use my
Tabs
class or its subclass
EnTab
.
Discussion
works a line at a time. For each character on the line, if the character is a space, we see if we
can coalesce it with previous spaces to output a single tab character. This program depends
on the
Tabs
class, which we'll come to shortly. The
Tabs
class is used to decide which
column positions represent tab stops and which do not. The code also has several
Debug
prin-
touts; these are controlled by an environment setting (see the online code in the
com.darwinsys.util.Debug
class).