See below.
There are several ways
to implement the idea of pushing back
a character.
The way that is the
most flexible is to
extend the BufferedReader
class to give it the
ability to push back
characters.
In fact,
the class PushbackReader
does
just that.
But it is interesting to see how simple
it is to implement the idea.
Let us create a class MyPushbackReader
that allows one character to be pushed back.
Our scanner always pushes back the same character. But many scanners push back different characters depending on which tokens are adjacent. A good design allows any character to be pushed back.
Do you think that pushing back more than one character at a time would ever be useful?