sdsu.io
Class Console

java.lang.Object
  |
  +--sdsu.io.Console

public class Console
extends java.lang.Object

This class is an extention of the console class found in Gary Cornell and Cay S. Horstmann, Core Java. The orginal class had 6 methods, this one has 40+. This class provides simple methods for reading from the stantard input and writing to the standard output. Input is done via statements like Console.readInt() and Console.readInt( "Prompt string" ). No exceptions are thrown on input. If there is a read error, the program will exit with a message. If you need to handle exceptions use sdsu.io.ASCIIInputStream. Output is done via Console.print( x ), Console.println( x ) or Console.print( "x = % .5f\n", x). The later takes a format string like printf in C. The format code has the following structure

Version:
1.1 14 Sept 1997
Author:
Roger Whitney (whitney@cs.sdsu.edu)

Constructor Summary
Console()
           
 
Method Summary
static boolean bad()
          Deprecated.  
static boolean checkError()
          Flushes the print stream and returns whether or not there was an error on the output stream.
static boolean eof()
          Determines if stream is has reach end of file.
static void flush()
          Forces the contents of the ouput buffer to be displayed on standard out.
static void flushLine()
          Causes the next I/O operation to start at the beginning of the next input line.
static void main(java.lang.String[] args)
          Example uses of this class
static void print(boolean value)
          Prints a boolean and insures that the contents of output buffer is printed on standard out.
static void print(char value)
          Prints a char and insures that the contents of output buffer is printed on standard out.
static void print(char[] value)
          Prints an array of characters and insures that the contents of output buffer is printed on standard out.
static void print(double value)
          Prints a double and insures that the contents of output buffer is printed on standard out.
static void print(float value)
          Prints a float and insures that the contents of output buffer is printed on standard out.
static void print(int value)
          Prints a integer and insures that the contents of output buffer is printed on standard out.
static void print(long value)
          Prints a long integer and insures that the contents of output buffer is printed on standard out.
static void print(java.lang.Object value)
          Prints an object and insures that the contents of output buffer is printed on standard out.
static void print(java.lang.String value)
          Prints a string and insures that the contents of output buffer is printed on standard out.
static void print(java.lang.String format, char value)
          prints a character following printf conventions
static void print(java.lang.String format, double value)
          prints a formatted double (and float) following printf conventions
static void print(java.lang.String format, long value)
          prints a formatted long integer (and integer) following printf conventions
static void print(java.lang.String format, java.lang.String value)
          prints a formatted string following printf conventions
static void println()
          Prints a newline then insures that the contents of output buffer is printed on standard out.
static void println(boolean value)
          Prints a boolean followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(char value)
          Prints a character followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(double value)
          Prints a double followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(float value)
          Prints a float followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(int value)
          Prints a integer followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(long value)
          Prints a long integer followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(java.lang.Object value)
          Prints a object followed by a newline then insures that the contents of output buffer is printed on standard out.
static void println(java.lang.String value)
          Prints a string followed by a newline then insures that the contents of output buffer is printed on standard out.
static boolean readBoolean()
          Reads an ASCII boolean value.
static boolean readBoolean(java.lang.String prompt)
          Reads an ASCII boolean value, after displaying given prompt.
static char readChar()
          Read an ASCII character and convert it into the internal char format.
static char readChar(java.lang.String prompt)
          Read an ASCII character and convert it into the internal char format, after displaying the given prompt.
static double readDouble()
          Reads an ASCII decimal floating point number.
static double readDouble(java.lang.String prompt)
          Reads an ASCII decimal floating point number, after displaying given prompt.
static float readFloat()
          Reads an ASCII decimal floating point number.
static float readFloat(java.lang.String prompt)
          Reads an ASCII decimal floating point number, after displaying given prompt.
static int readInt()
          Reads an ASCII decimal integer.
static int readInt(java.lang.String prompt)
          Reads an ASCII decimal integer, after displaying given prompt.
static java.lang.String readLine()
          Reads in a line that has been terminated by a \n, \r, \r\n or EOF.
static java.lang.String readLine(java.lang.String prompt)
          Reads in a line that has been terminated by a \n, \r, \r\n or EOF, after displaying the given prompt.
static long readLong()
          Reads an ASCII decimal long.
static long readLong(java.lang.String prompt)
          Reads an ASCII decimal long, after displaying the given prompt.
static short readShort()
          Reads an ASCII decimal short.
static short readShort(java.lang.String prompt)
          Reads an ASCII decimal short, after displaying given prompt.
static java.lang.String readWord()
          Reads a word.
static java.lang.String readWord(java.lang.String prompt)
          Reads a word, after displaying given prompt.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Console

public Console()
Method Detail

main

public static void main(java.lang.String[] args)
Example uses of this class

checkError

public static boolean checkError()
Flushes the print stream and returns whether or not there was an error on the output stream. Errors are cumulative; once the print stream encounters an error this routine will continue to return true on all successive calls.
Returns:
true if the print stream has ever encountered an error on the output stream.

flush

public static void flush()
Forces the contents of the ouput buffer to be displayed on standard out. Buffer then emptied.

print

public static void print(boolean value)
Prints a boolean and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the boolean to be printed

print

public static void print(char value)
Prints a char and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the char to be printed

print

public static void print(char[] value)
Prints an array of characters and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the array of character to be printed

print

public static void print(double value)
Prints a double and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the double to be printed

print

public static void print(float value)
Prints a float and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the float to be printed

print

public static void print(int value)
Prints a integer and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the integer to be printed

print

public static void print(long value)
Prints a long integer and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the long to be printed

print

public static void print(java.lang.Object value)
Prints an object and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the object to be printed

print

public static void print(java.lang.String value)
Prints a string and insures that the contents of output buffer is printed on standard out.
Parameters:
value - the string to be printed

print

public static void print(java.lang.String format,
                         char value)
prints a character following printf conventions
Parameters:
format - the format string
value - the character to print

print

public static void print(java.lang.String format,
                         double value)
prints a formatted double (and float) following printf conventions
Parameters:
format - the format string
value - the double to print

print

public static void print(java.lang.String format,
                         long value)
prints a formatted long integer (and integer) following printf conventions
Parameters:
format - the format string
value - the double to print

print

public static void print(java.lang.String format,
                         java.lang.String value)
prints a formatted string following printf conventions
Parameters:
format - the format string
value - the string to print

println

public static void println()
Prints a newline then insures that the contents of output buffer is printed on standard out.

println

public static void println(boolean value)
Prints a boolean followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the boolean to be printed

println

public static void println(char value)
Prints a character followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the character to be printed

println

public static void println(double value)
Prints a double followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the double to be printed

println

public static void println(float value)
Prints a float followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the float to be printed

println

public static void println(int value)
Prints a integer followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the integer to be printed

println

public static void println(long value)
Prints a long integer followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the long integer to be printed

println

public static void println(java.lang.Object value)
Prints a object followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the object to be printed

println

public static void println(java.lang.String value)
Prints a string followed by a newline then insures that the contents of output buffer is printed on standard out.
Parameters:
value - the string to be printed

bad

public static boolean bad()
Deprecated.  
Do not use. This method no longer does anything meaningful. Always returns false.

eof

public static boolean eof()
Determines if stream is has reach end of file. Consumes white space up to next token.
Returns:
true if end of file was reached or if only white space remained in the stream.

flushLine

public static void flushLine()
Causes the next I/O operation to start at the beginning of the next input line. Lines are delimited by either cr, cr lf, or lf.

readBoolean

public static boolean readBoolean()
Reads an ASCII boolean value. It reads a word and determines if it represents true or false. Possible values are: true and false. The comparison is case insensitive. Program exits on read errors
Returns:
the boolean.

readBoolean

public static boolean readBoolean(java.lang.String prompt)
Reads an ASCII boolean value, after displaying given prompt. It reads a word and determines if it represents true or false. Possible values are: true and false. The comparison is case insensitive. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the boolean.

readChar

public static char readChar()
Read an ASCII character and convert it into the internal char format. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Returns:
the character.

readChar

public static char readChar(java.lang.String prompt)
Read an ASCII character and convert it into the internal char format, after displaying the given prompt. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the character.

readDouble

public static double readDouble()
Reads an ASCII decimal floating point number. A floating point number is defined as follows: Doubles can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Program exits on read errors
Returns:
the double.

readDouble

public static double readDouble(java.lang.String prompt)
Reads an ASCII decimal floating point number, after displaying given prompt. A floating point number is defined as follows: Doubles can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the double.

readFloat

public static float readFloat()
Reads an ASCII decimal floating point number. A floating point number is defined as follows: Floats can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Program exits on read errors
Returns:
the float.

readFloat

public static float readFloat(java.lang.String prompt)
Reads an ASCII decimal floating point number, after displaying given prompt. A floating point number is defined as follows: Floats can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL.
Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the float.

readInt

public static int readInt()
Reads an ASCII decimal integer. Integers can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Returns:
the integer.

readInt

public static int readInt(java.lang.String prompt)
Reads an ASCII decimal integer, after displaying given prompt. Integers can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the integer.

readLine

public static java.lang.String readLine()
Reads in a line that has been terminated by a \n, \r, \r\n or EOF. The eof() method will return true if the read attempted to go beyond the end of file. The terminating line characters will not be part of the String that is returned. Program exits on read errors
Returns:
a String copy of the line or null if nothing more could be read.

readLine

public static java.lang.String readLine(java.lang.String prompt)
Reads in a line that has been terminated by a \n, \r, \r\n or EOF, after displaying the given prompt. The eof() method will return true if the read attempted to go beyond the end of file. The terminating line characters will not be part of the String that is returned. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
a String copy of the line or null if nothing more could be read.

readLong

public static long readLong()
Reads an ASCII decimal long. Longs can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Returns:
the long or 0 when EOF was reached.

readLong

public static long readLong(java.lang.String prompt)
Reads an ASCII decimal long, after displaying the given prompt. Longs can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the long or 0 when EOF was reached.

readShort

public static short readShort()
Reads an ASCII decimal short. Shorts can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Returns:
the short.
See Also:

readShort

public static short readShort(java.lang.String prompt)
Reads an ASCII decimal short, after displaying given prompt. Shorts can be preceded by optional whitespace. whitespace is defined as SPACE, TAB, CR, or NL. The eof() method will return true after this method has attempted to read beyond the end of file. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the short.
See Also:

readWord

public static java.lang.String readWord()
Reads a word. A word is a string of characters deliminated by whitespace or EOF. A word can be preceded by optional whitespace characters. Whitespace is defined as SPACE, TAB, CR, or NL.
Program exits on read errors
Returns:
the word or null if EOF reached.

readWord

public static java.lang.String readWord(java.lang.String prompt)
Reads a word, after displaying given prompt. A word is a string of characters deliminated by whitespace or EOF. A word can be preceded by optional whitespace characters. Whitespace is defined as SPACE, TAB, CR, or NL.
Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the word or null if EOF reached.