Class KeyboardReader

java.lang.Object
  |
  +--KeyboardReader

public class KeyboardReader
extends java.lang.Object

A class to read strings and numbers from the keyboard.

This class is intended for beginning Java programmers. It constructs the necessary buffered reader, converts strings to numbers, and handles exceptions.

The methods in this class are static methods. This means that they can be invoked using the class name, without needing to create a KeyboardReader object.

The following examples illustrate how to use this class to read from the keyboard:

    int x = KeyboardReader.readInt();        // read an int
    double y = KeyboardReader.readDouble();  // read a double
    String s = KeyboardReader.readString();    // read a String
 
This class also defines return values to indicate end-of-information (EOI) and data conversion errors.

The default behavior of the readInt() and readDouble() methods is to write a comment to the screen in response to inappropriate data. This behavior may be controlled by the ERROR_MESSAGES field.


Field Summary
static double EOI_DOUBLE
          Returned by the readDouble() method to indicate EOI.
static int EOI_INT
          Returned by the readInt() method to indicate EOI.
static java.lang.String EOI_STRING
          Returned by the readString() method to indicate EOI.
static double ERROR_DOUBLE
          Returned by the readDouble() method to indicate an error.
static int ERROR_INT
          Returned by the readInt() method to indicate an error.
static boolean ERROR_MESSAGES
          Controls the output of error messages to the console in response to inappropriate input.
static java.lang.String ERROR_STRING
          Returned by the readString() method to indicate an error.
 
Method Summary
static void main(java.lang.String[] args)
          Tests the KeyboardReader methods.
static double readDouble()
          Reads a line of input and converts it into a double.
static int readInt()
          Reads a line of input and converts it into an int.
static java.lang.String readString()
          Reads a line of character input.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOI_INT

public static final int EOI_INT
Returned by the readInt() method to indicate EOI.

EOI_DOUBLE

public static final double EOI_DOUBLE
Returned by the readDouble() method to indicate EOI.

EOI_STRING

public static final java.lang.String EOI_STRING
Returned by the readString() method to indicate EOI.

ERROR_INT

public static final int ERROR_INT
Returned by the readInt() method to indicate an error.

ERROR_DOUBLE

public static final double ERROR_DOUBLE
Returned by the readDouble() method to indicate an error.

ERROR_STRING

public static final java.lang.String ERROR_STRING
Returned by the readString() method to indicate an error.

ERROR_MESSAGES

public static boolean ERROR_MESSAGES
Controls the output of error messages to the console in response to inappropriate input.
Method Detail

readInt

public static int readInt()
Reads a line of input and converts it into an int.
Returns:
the integer that the user typed, or EOI_INT to indicate end-of-information, or ERROR_INT to indicate a conversion or I/O error.

readDouble

public static double readDouble()
Reads a line of input and converts it into a double.

Returns:
the number that the user typed, or EOI_DOUBLE to indicate end-of-information, or ERROR_DOUBLE to indicate a conversion or I/O error.

readString

public static java.lang.String readString()
Reads a line of character input.

Returns:
the line of input that the user typed, or EOI_STRING to indicate end-of-information, or ERROR_STRING to indicate a conversion or I/O error.

main

public static void main(java.lang.String[] args)
Tests the KeyboardReader methods.

This method would not normally be invoked by users of the KeyboardReader class.