Class Terminal

java.lang.Object
  extended byTerminal

public class Terminal
extends Object

Terminal provides a user-friendly interface to the standard System input and output streams (in, out, and err).

A Terminal is an object. In general, one is expected to instantiate just one Terminal. Although one might instantiate several, all will share the same System streams.

A Terminal may either explicitly echo input, or not. Echoing input is useful, for example, when testing with I/O redirection.

Inspired by Cay Horstmann's Console Class.


Constructor Summary
Terminal()
          Construct a Terminal that doesn't echo input.
Terminal(boolean echo)
          Construct a Terminal.
 
Method Summary
 void errPrint(boolean b)
          Print a Boolean value (true or false) to standard err (without a newline).
 void errPrint(char ch)
          Print character to standard err (without a newline).
 void errPrint(char[] s)
          Print character array to standard err (without a newline).
 void errPrint(double val)
          Print a double-precision floating point number to standard err (without a newline).
 void errPrint(float val)
          Print a floating point number to standard err (without a newline).
 void errPrint(int val)
          Print integer to standard err (without a newline).
 void errPrint(long val)
          Print a long integer to standard err (without a newline).
 void errPrint(Object val)
          Print Object to standard err (without a newline).
 void errPrint(String str)
          Print string to standard err (without a newline).
 void errPrintln()
          Print a newline to standard err, terminating the current line.
 void errPrintln(boolean b)
          Print a Boolean value (true or false) to standard err, followed by a newline.
 void errPrintln(char ch)
          Print character to standard err, followed by a newline.
 void errPrintln(char[] s)
          Print a character array to standard err, followed by a newline.
 void errPrintln(double val)
          Print a double-precision floating point number to standard err, followed by a newline.
 void errPrintln(float val)
          Print floating point number to standard err, followed by a newline.
 void errPrintln(int val)
          Print integer to standard err, followed by a newline.
 void errPrintln(long val)
          Print a long integer to standard err, followed by a newline.
 void errPrintln(Object val)
          Print Object to standard err, followed by a newline.
 void errPrintln(String str)
          Print string to standard err, followed by a newline.
static void main(String[] args)
          Unit test for Terminal.
 void print(boolean b)
          Print a Boolean value (true or false) to standard output (without a newline).
 void print(char ch)
          Print character to standard output (without a newline).
 void print(char[] s)
          Print character array to standard output (without a newline).
 void print(double val)
          Print a double-precision floating point number to standard output (without a newline).
 void print(float val)
          Print a floating point number to standard output (without a newline).
 void print(int val)
          Print integer to standard output (without a newline).
 void print(long val)
          Print a long integer to standard output (without a newline).
 void print(Object val)
          Print Object to standard output (without a newline).
 void print(String str)
          Print string to standard output (without a newline).
 void println()
          Print a newline to standard output, terminating the current line.
 void println(boolean b)
          Print a Boolean value (true or false) to standard output, followed by a newline.
 void println(char ch)
          Print character to standard output, followed by a newline.
 void println(char[] s)
          Print a character array to standard output, followed by a newline.
 void println(double val)
          Print a double-precision floating point number to standard output, followed by a newline.
 void println(float val)
          Print floating point number to standard output, followed by a newline.
 void println(int val)
          Print integer to standard output, followed by a newline.
 void println(long val)
          Print a long integer to standard output, followed by a newline.
 void println(Object val)
          Print Object to standard output, followed by a newline.
 void println(String str)
          Print string to standard output, followed by a newline.
 char readChar()
          Read a character from the Terminal.
 char readChar(String prompt)
          Read a character from the Terminal.
 char readCharOnce()
          Read a character from the Terminal.
 char readCharOnce(String prompt)
          Read a character from the Terminal.
 double readDouble()
          Read a double-precision floating point number, terminated by a newline, from the Terminal.
 double readDouble(String prompt)
          Read a double-precision floating point number, terminated by a newline, from the Terminal.
 double readDoubleOnce()
          Read a double-precision floating point number, terminated by a newline, from the Terminal.
 double readDoubleOnce(String prompt)
          Read a double-precision floating point number, terminated by a newline, from the Terminal.
 int readInt()
          Read an integer, terminated by a new line, from the Terminal.
 int readInt(String prompt)
          Read an integer, terminated by a new line, from the Terminal.
 int readIntOnce()
          Read an integer, terminated by a new line, from the Terminal.
 int readIntOnce(String prompt)
          Read an integer, terminated by a new line, from the Terminal.
 String readLine()
          Read a line (terminated by a newline) from the Terminal.
 String readLine(String prompt)
          Read a line (terminated by a newline) from the Terminal.
 String readWord()
          Read a word from the Terminal.
 String readWord(String prompt)
          Read a word from the Terminal.
 String readWordOnce()
          Read a word from the Terminal.
 String readWordOnce(String prompt)
          Read a word from the Terminal.
 boolean readYesOrNo()
          Read "yes" or "no" from the Terminal.
 boolean readYesOrNo(String prompt)
          Read "yes" or "no" from the Terminal.
 boolean readYesOrNoOnce()
          Read "yes" or "no" from the Terminal.
 boolean readYesOrNoOnce(String prompt)
          Read "yes" or "no" from the Terminal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Terminal

public Terminal()
Construct a Terminal that doesn't echo input.


Terminal

public Terminal(boolean echo)
Construct a Terminal.

Parameters:
echo - whether or not input should be echoed.
Method Detail

errPrint

public void errPrint(boolean b)
Print a Boolean value (true or false) to standard err (without a newline).

Parameters:
b - Boolean to print.

errPrint

public void errPrint(char ch)
Print character to standard err (without a newline).

Parameters:
ch - character to print.

errPrint

public void errPrint(char[] s)
Print character array to standard err (without a newline).

Parameters:
s - character array to print.

errPrint

public void errPrint(double val)
Print a double-precision floating point number to standard err (without a newline).

Parameters:
val - number to print.

errPrint

public void errPrint(float val)
Print a floating point number to standard err (without a newline).

Parameters:
val - number to print.

errPrint

public void errPrint(int val)
Print integer to standard err (without a newline).

Parameters:
val - integer to print.

errPrint

public void errPrint(long val)
Print a long integer to standard err (without a newline).

Parameters:
val - integer to print.

errPrint

public void errPrint(Object val)
Print Object to standard err (without a newline).

Parameters:
val - Object to print.

errPrint

public void errPrint(String str)
Print string to standard err (without a newline).

Parameters:
str - String to print.

errPrintln

public void errPrintln()
Print a newline to standard err, terminating the current line.


errPrintln

public void errPrintln(boolean b)
Print a Boolean value (true or false) to standard err, followed by a newline.

Parameters:
b - Boolean to print.

errPrintln

public void errPrintln(char ch)
Print character to standard err, followed by a newline.

Parameters:
ch - character to print.

errPrintln

public void errPrintln(char[] s)
Print a character array to standard err, followed by a newline.

Parameters:
s - character array to print.

errPrintln

public void errPrintln(double val)
Print a double-precision floating point number to standard err, followed by a newline.

Parameters:
val - number to print.

errPrintln

public void errPrintln(float val)
Print floating point number to standard err, followed by a newline.

Parameters:
val - number to print.

errPrintln

public void errPrintln(int val)
Print integer to standard err, followed by a newline.

Parameters:
val - integer to print.

errPrintln

public void errPrintln(long val)
Print a long integer to standard err, followed by a newline.

Parameters:
val - long integer to print.

errPrintln

public void errPrintln(Object val)
Print Object to standard err, followed by a newline.

Parameters:
val - Object to print

errPrintln

public void errPrintln(String str)
Print string to standard err, followed by a newline.

Parameters:
str - String to print

main

public static void main(String[] args)
Unit test for Terminal.

Parameters:
args - command line arguments:
    -e echo all input.
 

print

public void print(boolean b)
Print a Boolean value (true or false) to standard output (without a newline).

Parameters:
b - Boolean to print.

print

public void print(char ch)
Print character to standard output (without a newline).

Parameters:
ch - character to print.

print

public void print(char[] s)
Print character array to standard output (without a newline).

Parameters:
s - character array to print.

print

public void print(double val)
Print a double-precision floating point number to standard output (without a newline).

Parameters:
val - number to print.

print

public void print(float val)
Print a floating point number to standard output (without a newline).

Parameters:
val - number to print.

print

public void print(int val)
Print integer to standard output (without a newline).

Parameters:
val - integer to print.

print

public void print(long val)
Print a long integer to standard output (without a newline).

Parameters:
val - integer to print.

print

public void print(Object val)
Print Object to standard output (without a newline).

Parameters:
val - Object to print.

print

public void print(String str)
Print string to standard output (without a newline).

Parameters:
str - String to print.

println

public void println()
Print a newline to standard output, terminating the current line.


println

public void println(boolean b)
Print a Boolean value (true or false) to standard output, followed by a newline.

Parameters:
b - Boolean to print.

println

public void println(char ch)
Print character to standard output, followed by a newline.

Parameters:
ch - character to print.

println

public void println(char[] s)
Print a character array to standard output, followed by a newline.

Parameters:
s - character array to print.

println

public void println(double val)
Print a double-precision floating point number to standard output, followed by a newline.

Parameters:
val - number to print.

println

public void println(float val)
Print floating point number to standard output, followed by a newline.

Parameters:
val - number to print.

println

public void println(int val)
Print integer to standard output, followed by a newline.

Parameters:
val - integer to print.

println

public void println(long val)
Print a long integer to standard output, followed by a newline.

Parameters:
val - long integer to print.

println

public void println(Object val)
Print Object to standard output, followed by a newline.

Parameters:
val - Object to print

println

public void println(String str)
Print string to standard output, followed by a newline.

Parameters:
str - String to print

readChar

public char readChar()
Read a character from the Terminal. Prompt again when an empty line is read.

Returns:
the character read.

readChar

public char readChar(String prompt)
Read a character from the Terminal. Prompt again when an empty line is read.

Parameters:
prompt - output string to prompt for input.
Returns:
the character read.

readCharOnce

public char readCharOnce()
Read a character from the Terminal. Throw an exception if an empty line is read.

Returns:
the character read.
Throws:
RuntimeException - if it reads an empty line.

readCharOnce

public char readCharOnce(String prompt)
Read a character from the Terminal. Throw an exception if an empty line is read.

Parameters:
prompt - output string to prompt for input.
Returns:
the character read.
Throws:
RuntimeException - if it reads an empty line.

readDouble

public double readDouble()
Read a double-precision floating point number, terminated by a newline, from the Terminal. If a NumberFormatException is encountered, try again.

Returns:
the input value as a double.

readDouble

public double readDouble(String prompt)
Read a double-precision floating point number, terminated by a newline, from the Terminal. If a NumberFormatException is encountered, try again.

Parameters:
prompt - output string to prompt for input.
Returns:
the input value as a double.

readDoubleOnce

public double readDoubleOnce()
                      throws NumberFormatException
Read a double-precision floating point number, terminated by a newline, from the Terminal.

Returns:
the input value as a double.
Throws:
NumberFormatException - for a badly formed number.

readDoubleOnce

public double readDoubleOnce(String prompt)
                      throws NumberFormatException
Read a double-precision floating point number, terminated by a newline, from the Terminal.

Parameters:
prompt - output string to prompt for input.
Returns:
the input value as a double.
Throws:
NumberFormatException - for a badly formed number.

readInt

public int readInt()
Read an integer, terminated by a new line, from the Terminal. If a NumberFormatException is encountered, try again.

Returns:
the input value as an int.

readInt

public int readInt(String prompt)
Read an integer, terminated by a new line, from the Terminal. If a NumberFormatException is encountered, try again.

Parameters:
prompt - output string to prompt for input.
Returns:
the input value as an int.

readIntOnce

public int readIntOnce()
                throws NumberFormatException
Read an integer, terminated by a new line, from the Terminal.

Returns:
the input value as an int.
Throws:
NumberFormatException - for a badly formed integer.

readIntOnce

public int readIntOnce(String prompt)
                throws NumberFormatException
Read an integer, terminated by a new line, from the Terminal.

Parameters:
prompt - output string to prompt for input.
Returns:
the input value as an int.
Throws:
NumberFormatException - for a badly formed integer.

readLine

public String readLine()
Read a line (terminated by a newline) from the Terminal.

Returns:
the string (without the newline character).

readLine

public String readLine(String prompt)
Read a line (terminated by a newline) from the Terminal.

Parameters:
prompt - output string to prompt for input.
Returns:
the string (without the newline character), null if eof.

readWord

public String readWord()
Read a word from the Terminal. If an empty line is entered, try again. Words are terminated by whitespace. Leading whitespace is trimmed; the rest of the line is disposed of.

Returns:
the word read.

readWord

public String readWord(String prompt)
Read a word from the Terminal. If an empty line is entered, try again. Words are terminated by whitespace. Leading whitespace is trimmed; the rest of the line is disposed of.

Parameters:
prompt - output string to prompt for input.
Returns:
the word read.

readWordOnce

public String readWordOnce()
Read a word from the Terminal. If an empty line is entered, throw an exception. Words are terminated by whitespace. Leading whitespace is trimmed; the rest of the line is disposed of.

Returns:
the word read.
Throws:
RuntimeException - if it reads an empty line.

readWordOnce

public String readWordOnce(String prompt)
Read a word from the Terminal. If an empty line is entered, throw an exception. Words are terminated by whitespace. Leading whitespace is trimmed; the rest of the line is disposed of.

Parameters:
prompt - output string to prompt for input.
Returns:
the word read.
Throws:
RuntimeException - if it reads an empty line.

readYesOrNo

public boolean readYesOrNo()
Read "yes" or "no" from the Terminal. If an empty line or improper character is read, try again. No prompting is done. Look only at first character and accept any case.

Returns:
true if yes, false if no.

readYesOrNo

public boolean readYesOrNo(String prompt)
Read "yes" or "no" from the Terminal. If an empty line or improper character is read, try again. Look only at first character and accept any case.

Parameters:
prompt - output string to prompt for input.
Returns:
true if yes, false if no.

readYesOrNoOnce

public boolean readYesOrNoOnce()
Read "yes" or "no" from the Terminal. If an empty line or improper character is read, throw an exception. Look only at first character and accept any case.

Returns:
true if yes, false if no.
Throws:
RuntimeException - on improper input.

readYesOrNoOnce

public boolean readYesOrNoOnce(String prompt)
Read "yes" or "no" from the Terminal. If an empty line or improper character is read, throw an exception. Look only at first character and accept any case.

Parameters:
prompt - output string to prompt for input.
Returns:
true if yes, false if no.
Throws:
RuntimeException - on improper input.