Java Reference
In-Depth Information
an integer, a string, or a floating-point number, the programmer has to de-
veloparathercomplicatedroutinebasedonthemethodsandsubclassesof
the Java InputStream class, which is part of the java.io library. Alterna-
tively, you could develop an input routine using the methods of the System
class, which is part of the java.lang library, but it is also complicated and
beyond your present understanding of the language.
Although the processing required for obtaining keyboard input is not
difficult to implement, it is beyond our present level. On the other hand,
even the simplest program requires some form of data input. It would be
impossible to perform any kind of non-trivial programming in Java with-
out means for obtaining keyboard data. For this reason we have devel-
oped a class to perform data input. The class, called Keyin, contains the
following methods:
inChar() inputs a single keyboard character.
inString() inputs a string from the keyboard.
inInt() inputs an integer number.
inDouble() inputs a float in double format.
To use the methods inChar(), inString(), inInt(), and inDouble() you
must have the Keyin class accessible to the code. The easiest way is to
copy the file Keyin.class to your current development directory. This
done, your program will be able to input characters, strings, integers, and
floating-point numbers.
Each of the methods in the Keyin class displays a prompt message to
inform the user of the input required. This message is passed as a parame-
ter, as in the following code fragment:
int age1;
...
age1 = inInt(“Please enter your age” );
When the method inInt() executes, the prompt message “Please enter
your age: ” is displayed on the screen. Once the user enters a valid value
and presses the <Enter> key the value is assigned to the variable age1.
The methods inChar(), inString() and inDouble() are similar.
On the Web
You may lookat the code of the Keyin class by loading the file
Keyin.java into your editor program. Keyin.java is found in the Chap-
ter 18 folder at www.crcpress.com.
Search WWH ::




Custom Search