a read() method as standard, and that's it. If you want more than that, reading integers, or decimal values,
or strings as keyboard input, you're on your own. Let's see what you can do to remedy that.
Getting Data from the Keyboard
To get sensible input from the keyboard, you have to be able to scan the stream of characters and recognize
what they are. When you read a numerical value from the stream, you have to look for the digits and possibly
the sign and decimal point, figure out where the number starts and ends in the stream, and finally convert it
to the appropriate value. To write the code to do this from scratch would take quite a lot of work. Fortunately,
you can get a lot of help from the class libraries. One possibility is to use the java.util.Scanner class, but
I defer discussion of that until Chapter 15 because you need to understand another topic before you can use
Scanner objects effectively. The StreamTokenizer class in the java.io package is another possibility, so
let's look further into that.
The term token refers to a data item such as a number or a string that, in general, consists of several con-
secutive characters of a particular kind from the stream. For example, a number is usually a sequence of
characters that consists of digits, maybe a decimal point, and sometimes a sign in front. The class has the
name StreamTokenizer because it can read characters from a stream and parse it into a series of tokens that
You create a StreamTokenizer object from a stream reader object that reads data from the underlying
input stream. To read the standard input stream System.in you can use an InputStreamReader object that
converts the raw bytes that are read from the stream from the local character encoding to Unicode characters
before the StreamTokenizer object sees them. In the interest of efficiency it would be a good idea to buffer
the data from the InputStreamReader through a BufferedReader object that buffers the data in memory.
With this in mind, you could create a StreamTokenizer object like this:
StreamTokenizer tokenizer = new StreamTokenizer(
The argument to the StreamTokenizer object is the original standard input stream System.in inside an
InputStreamReader object that converts the bytes to Unicode inside a BufferedReader object that sup-
plies the stream of Unicode characters via a buffer in memory.
Before you can make use of the StreamTokenizer object for keyboard input, you need to understand a
bit more about how it works.
Tokenizing a Stream
The StreamTokenizer class defines objects that can read an input stream and parse it into tokens. The input
stream is read and treated as a series of separate bytes, and each byte is regarded as a Unicode character in
the range 'u\0000' to 'u\00FF' . A StreamTokenizer object in its default state can recognize the follow-
A sequence consisting of the digits 0 to 9, plus possibly a decimal point, and a + or − sign.
Any sequence of characters between a pair of single quotes or a pair of double quotes.