Class Keyboard

java.lang.Object
  |
  +--Keyboard

public class Keyboard
extends java.lang.Object

The Keyboard class, which simplifies input from a keyboard, is based on James Slack's Programming and Problem Solving with Java, published by Brooks/Cole. I have extended it to provide methods that take default values and, therefore, do not throw exceptions. The methods that read various types of floating point numbers have not yet been implemented.

The class file for Slack's original class is available online at

http://krypton.mankato.msus.edu/~slack/javabook/software.html

Author:
James Slack, Minnesota State University; modified by Alyce Brady

Constructor Summary
Keyboard()
           
 
Method Summary
static int readInt()
          Waits for the user to type a number, and then returns that number as an int.
static int readInt(int defaultValue)
          Waits for the user to type a number, and then returns that number as an int.
static int readInt(java.lang.String prompt)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int.
static int readInt(java.lang.String prompt, int defaultValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int.
static int readInt(java.lang.String prompt, int lowValue, int highValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int.
static int readInt(java.lang.String prompt, int lowValue, int highValue, int defaultValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int.
static long readLong()
          Waits for the user to type a number, and then returns that number as a long.
static long readLong(long defaultValue)
          Waits for the user to type a number, and then returns that number as a long.
static long readLong(java.lang.String prompt)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long.
static long readLong(java.lang.String prompt, int lowValue, int highValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long.
static long readLong(java.lang.String prompt, int lowValue, int highValue, long defaultValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long.
static long readLong(java.lang.String prompt, long defaultValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long.
static short readShort()
          Waits for the user to type a number, and then returns that number as a short.
static short readShort(short defaultValue)
          Waits for the user to type a number, and then returns that number as a short.
static short readShort(java.lang.String prompt)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short.
static short readShort(java.lang.String prompt, int lowValue, int highValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short.
static short readShort(java.lang.String prompt, int lowValue, int highValue, short defaultValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short.
static short readShort(java.lang.String prompt, short defaultValue)
          Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short.
static java.lang.String readString()
          Waits for the user to type a line, and then returns all characters the user typed as a string.
static java.lang.String readString(java.lang.String prompt)
          Displays the given prompt, and then waits for the user to type a line, and then returns all characters the user typed as a string.
static java.lang.String readString(java.lang.String prompt, java.lang.String defaultValue)
          Displays the given prompt, and then waits for the user to type a line, and then returns all characters the user typed as a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Keyboard

public Keyboard()
Method Detail

readString

public static java.lang.String readString()
                                   throws java.io.IOException
Waits for the user to type a line, and then returns all characters the user typed as a string.
Returns:
a String containing the characters the user typed in up to (not including) a newline.
Throws:
java.io.IOException - i/o error when reading from console

readString

public static java.lang.String readString(java.lang.String prompt)
                                   throws java.io.IOException
Displays the given prompt, and then waits for the user to type a line, and then returns all characters the user typed as a string.
Parameters:
prompt - user prompt
Returns:
a String containing the characters the user typed in up to (not including) a newline.
Throws:
java.io.IOException - i/o error when reading from console

readString

public static java.lang.String readString(java.lang.String prompt,
                                          java.lang.String defaultValue)
Displays the given prompt, and then waits for the user to type a line, and then returns all characters the user typed as a string.
Parameters:
prompt - user prompt
Returns:
a String containing the characters the user typed in up to (not including) a newline, or defaultValue if there was an I/O error

readLong

public static long readLong()
                     throws java.io.IOException
Waits for the user to type a number, and then returns that number as a long. If the user enters an invalid response, the program prompts the user to retype the response.
Returns:
the number typed in by the user as a long
Throws:
java.io.IOException - i/o error when reading from console

readLong

public static long readLong(long defaultValue)
Waits for the user to type a number, and then returns that number as a long. If the user enters an invalid response, the program prompts the user to retype the response.
Returns:
the number typed in by the user as a long, or defaultValue if there was an I/O error

readLong

public static long readLong(java.lang.String prompt)
                     throws java.io.IOException
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long. If the user enters an invalid response, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a long
Throws:
java.io.IOException - i/o error when reading from console

readLong

public static long readLong(java.lang.String prompt,
                            long defaultValue)
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long. If the user enters an invalid response, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a long, or defaultValue if there was an I/O error

readLong

public static long readLong(java.lang.String prompt,
                            int lowValue,
                            int highValue)
                     throws java.io.IOException
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long. If the user enters an invalid response, the program prompts the user to retype the response. If the user types a value outside the range from lowValue to highValue, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a long
Throws:
java.io.IOException - i/o error when reading from console
Precondition:
highValue >= lowValue

readLong

public static long readLong(java.lang.String prompt,
                            int lowValue,
                            int highValue,
                            long defaultValue)
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a long. If the user enters an invalid response, the program prompts the user to retype the response. If the user types a value outside the range from lowValue to highValue, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a long, or defaultValue if there was an I/O error
Precondition:
highValue >= lowValue

readInt

public static int readInt()
                   throws java.io.IOException
Waits for the user to type a number, and then returns that number as an int. If the user enters an invalid response, the program prompts the user to retype the response.
Returns:
the number typed in by the user as an int
Throws:
java.io.IOException - i/o error when reading from console

readInt

public static int readInt(int defaultValue)
Waits for the user to type a number, and then returns that number as an int. If the user enters an invalid response, the program prompts the user to retype the response.
Returns:
the number typed in by the user as an int, or defaultValue if there was an I/O error

readInt

public static int readInt(java.lang.String prompt)
                   throws java.io.IOException
Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int. If the user enters an invalid response, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as an int
Throws:
java.io.IOException - i/o error when reading from console

readInt

public static int readInt(java.lang.String prompt,
                          int defaultValue)
Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int. If the user enters an invalid response, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as an int, or defaultValue if there was an I/O error

readInt

public static int readInt(java.lang.String prompt,
                          int lowValue,
                          int highValue)
                   throws java.io.IOException
Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int. If the user enters an invalid response, the program prompts the user to retype the response. If the user types a value outside the range from lowValue to highValue, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as an int
Throws:
java.io.IOException - i/o error when reading from console
Precondition:
highValue >= lowValue

readInt

public static int readInt(java.lang.String prompt,
                          int lowValue,
                          int highValue,
                          int defaultValue)
Displays the given prompt, and then waits for the user to type a number, and then returns that number as an int. If the user enters an invalid response, the program prompts the user to retype the response. If the user types a value outside the range from lowValue to highValue, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as an int, or defaultValue if there was an I/O error
Precondition:
highValue >= lowValue

readShort

public static short readShort()
                       throws java.io.IOException
Waits for the user to type a number, and then returns that number as a short. If the user enters an invalid response, the program prompts the user to retype the response.
Returns:
the number typed in by the user as a short
Throws:
java.io.IOException - i/o error when reading from console

readShort

public static short readShort(short defaultValue)
Waits for the user to type a number, and then returns that number as a short. If the user enters an invalid response, the program prompts the user to retype the response.
Returns:
the number typed in by the user as a short, or defaultValue if there was an I/O error

readShort

public static short readShort(java.lang.String prompt)
                       throws java.io.IOException
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short. If the user enters an invalid response, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a short
Throws:
java.io.IOException - i/o error when reading from console

readShort

public static short readShort(java.lang.String prompt,
                              short defaultValue)
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short. If the user enters an invalid response, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a short, or defaultValue if there was an I/O error

readShort

public static short readShort(java.lang.String prompt,
                              int lowValue,
                              int highValue)
                       throws java.io.IOException
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short. If the user enters an invalid response, the program prompts the user to retype the response. If the user types a value outside the range from lowValue to highValue, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a short
Throws:
java.io.IOException - i/o error when reading from console
Precondition:
highValue >= lowValue

readShort

public static short readShort(java.lang.String prompt,
                              int lowValue,
                              int highValue,
                              short defaultValue)
Displays the given prompt, and then waits for the user to type a number, and then returns that number as a short. If the user enters an invalid response, the program prompts the user to retype the response. If the user types a value outside the range from lowValue to highValue, the program prompts the user to retype the response.
Parameters:
prompt - user prompt
Returns:
the number typed in by the user as a short, or defaultValue if there was an I/O error
Precondition:
highValue >= lowValue