Class sdsu.io.Console
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class sdsu.io.Console

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

public class Console
extends 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
Author:
Roger Whitney (whitney@cs.sdsu.edu)

Constructor Index

 o Console()

Method Index

 o bad()
Determines if a previous ASCII I/O operation caused an error.
 o checkError()
Flushes the print stream and returns whether or not there was an error on the output stream.
 o eof()
Determines if a previous ASCII I/O operation caught End Of File.
 o flush()
Forces the contents of the ouput buffer to be displayed on standard out.
 o flushLine()
Causes the next I/O operation to start at the beginning of the next input line.
 o main(String[])
Example uses of this class
 o print(boolean)
Prints a boolean and insures that the contents of output buffer is printed on standard out.
 o print(char)
Prints a char and insures that the contents of output buffer is printed on standard out.
 o print(char[])
Prints an array of characters and insures that the contents of output buffer is printed on standard out.
 o print(double)
Prints a double and insures that the contents of output buffer is printed on standard out.
 o print(float)
Prints a float and insures that the contents of output buffer is printed on standard out.
 o print(int)
Prints a integer and insures that the contents of output buffer is printed on standard out.
 o print(long)
Prints a long integer and insures that the contents of output buffer is printed on standard out.
 o print(Object)
Prints an object and insures that the contents of output buffer is printed on standard out.
 o print(String)
Prints a string and insures that the contents of output buffer is printed on standard out.
 o print(String, char)
prints a character following printf conventions
 o print(String, double)
prints a formatted double (and float) following printf conventions
 o print(String, long)
prints a formatted long integer (and integer) following printf conventions
 o print(String, String)
prints a formatted string following printf conventions
 o println()
Prints a newline then insures that the contents of output buffer is printed on standard out.
 o println(boolean)
Prints a boolean followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(char)
Prints a character followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(double)
Prints a double followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(float)
Prints a float followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(int)
Prints a integer followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(long)
Prints a long integer followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(Object)
Prints a object followed by a newline then insures that the contents of output buffer is printed on standard out.
 o println(String)
Prints a string followed by a newline then insures that the contents of output buffer is printed on standard out.
 o printPrompt(String)
Displays standard out a string prompt followed by a space
 o readBoolean()
Reads an ASCII boolean value.
 o readBoolean(String)
Reads an ASCII boolean value, after displaying given prompt.
 o readChar()
Read an ASCII character and convert it into the internal char format.
 o readChar(String)
Read an ASCII character and convert it into the internal char format, after displaying the given prompt.
 o readDouble()
Reads an ASCII decimal floating point number.
 o readDouble(String)
Reads an ASCII decimal floating point number, after displaying given prompt.
 o readFloat()
Reads an ASCII decimal floating point number.
 o readFloat(String)
Reads an ASCII decimal floating point number, after displaying given prompt.
 o readInt()
Reads an ASCII decimal integer.
 o readInt(String)
Reads an ASCII decimal integer, after displaying given prompt.
 o readLine()
Reads in a line that has been terminated by a \n, \r, \r\n or EOF.
 o readLine(String)
Reads in a line that has been terminated by a \n, \r, \r\n or EOF, after displaying the given prompt.
 o readLong()
Reads an ASCII decimal long.
 o readLong(String)
Reads an ASCII decimal long, after displaying the given prompt.
 o readShort()
Reads an ASCII decimal short.
 o readShort(String)
Reads an ASCII decimal short, after displaying given prompt.
 o readWord()
Reads a word.
 o readWord(String)
Reads a word, after displaying given prompt.

Constructors

 o Console
  public Console()

Methods

 o main
  public static void main(String args[])
Example uses of this class
 o 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.
 o flush
  public static void flush()
Forces the contents of the ouput buffer to be displayed on standard out. Buffer then emptied.
 o printPrompt
  protected static void printPrompt(String prompt)
Displays standard out a string prompt followed by a space
Parameters:
prompt - string displayed on standard out
 o 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
 o 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
 o 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
 o 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
 o 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
 o 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
 o 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
 o print
  public static void print(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
 o print
  public static void print(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
 o print
  public static void print(String format,
                           char value)
prints a character following printf conventions
Parameters:
format - the format string
value - the character to print
 o print
  public static void print(String format,
                           double value)
prints a formatted double (and float) following printf conventions
Parameters:
format - the format string
value - the double to print
 o print
  public static void print(String format,
                           long value)
prints a formatted long integer (and integer) following printf conventions
Parameters:
format - the format string
value - the double to print
 o print
  public static void print(String format,
                           String value)
prints a formatted string following printf conventions
Parameters:
format - the format string
value - the string to print
 o println
  public static void println()
Prints a newline then insures that the contents of output buffer is printed on standard out.
 o 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
 o 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
 o 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
 o 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
 o 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
 o 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
 o println
  public static void println(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
 o println
  public static void println(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
 o bad
  public static boolean bad()
Determines if a previous ASCII I/O operation caused an error. Errors can occur in the reading of numbers.
Returns:
true if there was an error.
See Also:
bad
 o eof
  public static boolean eof()
Determines if a previous ASCII I/O operation caught End Of File.
Returns:
true if end of file was reached.
 o 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.
 o readBoolean
  public static boolean readBoolean()
Reads an ASCII boolean value. It reads a word and determines if it represents true or false. Possible values for false are: All other words are considered true. 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 boolean.
 o readBoolean
  public static boolean readBoolean(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 for false are: All other words are considered true. 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.
 o 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.
 o readChar
  public static char readChar(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.
 o 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.
If an error on the format of the number is found, the bad() member will return true. Program exits on read errors
Returns:
the double.
 o readDouble
  public static double readDouble(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.
If an error on the format of the number is found, the bad() member will return true. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the double.
 o 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.
If an error on the format of the number is found, the bad() member will return true. Program exits on read errors
Returns:
the float.
 o readFloat
  public static float readFloat(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.
If an error on the format of the number is found, the bad() member will return true. Program exits on read errors
Parameters:
prompt - the prompt string to display
Returns:
the float.
 o 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.
 o readInt
  public static int readInt(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.
 o readLine
  public static 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.
 o readLine
  public static String readLine(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.
 o 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.
 o readLong
  public static long readLong(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.
 o 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:
ASCIIInputStream@readInt
 o readShort
  public static short readShort(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:
ASCIIInputStream@readInt
 o readWord
  public static 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.
 o readWord
  public static String readWord(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.

All Packages  Class Hierarchy  This Package  Previous  Next  Index