import java.io.*; // For data input streams.
// **************************************************************
/** Class to handle keyboard input. Prompts for user's keyboard
* input and converts input string into numbers. Picks up
* conversion errors.
* @author Jo Wood.
* @version 1.4, 30th May, 2001
*/
// **************************************************************
public class KeyboardInput
{
// ----------------- Object variables ------------------
private String textLine; // Line of text input from keyboard.
// ------------------- Constructor ---------------------
/** Creates a keyboardInput object that handles prompting
* and conversion of keyboard input.
*/
public KeyboardInput()
{
textLine = new String(); // Create empty string.
}
// --------------------- Methods ----------------------
/** Prompts the user to type in something from the keyboard.
* Uses standard input to gether input.
* @param promptText Message to prompt input with.
*/
public void prompt(String promptText)
{
System.out.print(promptText + " ");
textLine = readInput();
}
/** Extracts an integer number from the keyboard input.
* @return Integer value extracted from keyboard input.
*/
public int getInt()
{
int intVal=0;
try
{
intVal = new Integer(textLine).intValue();
}
catch (NumberFormatException e)
{
System.err.println("Error converting input to integer.");
}
return intVal;
}
/** Extracts a floating point number from the keyboard input.
* @return Floating point value extracted from keyboard input.
*/
public float getFloat()
{
float floatVal=0.0f;
try
{
floatVal = new Float(textLine).floatValue();
}
catch (NumberFormatException e)
{
System.err.println("Error converting input to floating point number.");
}
return floatVal;
}
/** Extracts a double precision number from the keyboard input.
* @return Double precision value extracted from keyboard input.
*/
public double getDouble()
{
double doubleVal=0.0;
try
{
doubleVal = new Double(textLine).doubleValue();
}
catch (NumberFormatException e)
{
System.err.println("Error converting input to double precision number.");
}
return doubleVal;
}
/** Extracts a string typed in on the keyboard.
* @return String extracted from keyboard input.
*/
public String getString()
{
return textLine;
}
/** Extracts the first letter of a string typed in on the
* keyboard.
* @return First character extracted from keyboard input.
*/
public char getChar()
{
char firstChar = ' ';
try
{
firstChar = textLine.charAt(0);
}
catch (StringIndexOutOfBoundsException e)
{
System.err.println("Error finding first letter of input.");
}
return firstChar;
}
// ------------------- Private Methods -----------------
/** Returns a line of text input from standard input (usually
* the keyboard).
* @return Line of keyboard input.
*/
private String readInput()
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
textLine = in.readLine();
}
catch (IOException e)
{
System.err.println("Problem reading keyboard input");
return null;
}
return textLine;
}
}