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
- a % (required)
- a modifier (optional)
- +
- forces display of + for positive numbers
- 0
- show leading zeroes
- -
- align left in the field
- space
- prepend a space in front of positive numbers
- #
- use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format.
- an integer denoting field width (optional)
- a period followed by an integer denoting precision (optional)
- a format descriptor (required)
- f
- floating point number in fixed format
- e, E
- floating point number in exponential notation (scientific format). The E format results in an uppercase E for the exponent (1.14130E+003), the e format in a lowercase e.
- g, G
- floating point number in general format (fixed format for small numbers, exponential format for large numbers). Trailing zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format in a lowercase e.
- d, i
- integer in decimal
- x
- integer in hexadecimal
- o
- integer in octal
- s
- string
- c
- character
- Author:
- Roger Whitney (whitney@cs.sdsu.edu)
-
Console()
-
-
bad()
- Determines if a previous ASCII I/O operation caused an error.
-
checkError()
- Flushes the print stream and returns whether or not there was
an error on the output stream.
-
eof()
- Determines if a previous ASCII I/O operation caught End Of File.
-
flush()
- Forces the contents of the ouput buffer to be displayed on standard out.
-
flushLine()
- Causes the next I/O operation to start at the beginning of the next
input line.
-
main(String[])
- Example uses of this class
-
print(boolean)
- Prints a boolean and insures that the
contents of output buffer is printed on standard out.
-
print(char)
- Prints a char and insures that the
contents of output buffer is printed on standard out.
-
print(char[])
- Prints an array of characters and insures that the
contents of output buffer is printed on standard out.
-
print(double)
- Prints a double and insures that the
contents of output buffer is printed on standard out.
-
print(float)
- Prints a float and insures that the
contents of output buffer is printed on standard out.
-
print(int)
- Prints a integer and insures that the
contents of output buffer is printed on standard out.
-
print(long)
- Prints a long integer and insures that the
contents of output buffer is printed on standard out.
-
print(Object)
- Prints an object and insures that the
contents of output buffer is printed on standard out.
-
print(String)
- Prints a string and insures that the
contents of output buffer is printed on standard out.
-
print(String, char)
-
prints a character following printf conventions
-
print(String, double)
-
prints a formatted double (and float) following printf conventions
-
print(String, long)
-
prints a formatted long integer (and integer) following printf conventions
-
print(String, String)
-
prints a formatted string following printf conventions
-
println()
- Prints a newline then insures that the
contents of output buffer is printed on standard out.
-
println(boolean)
- Prints a boolean followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(char)
- Prints a character followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(double)
- Prints a double followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(float)
- Prints a float followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(int)
- Prints a integer followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(long)
- Prints a long integer followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(Object)
- Prints a object followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
println(String)
- Prints a string followed by a newline then insures that the
contents of output buffer is printed on standard out.
-
printPrompt(String)
- Displays standard out a string prompt followed by a space
-
readBoolean()
- Reads an ASCII boolean value.
-
readBoolean(String)
- Reads an ASCII boolean value, after displaying given prompt.
-
readChar()
- Read an ASCII character and convert it into the internal char
format.
-
readChar(String)
- Read an ASCII character and convert it into the internal char
format, after displaying the given prompt.
-
readDouble()
- Reads an ASCII decimal floating point number.
-
readDouble(String)
- Reads an ASCII decimal floating point number, after displaying given prompt.
-
readFloat()
- Reads an ASCII decimal floating point number.
-
readFloat(String)
- Reads an ASCII decimal floating point number, after displaying given prompt.
-
readInt()
- Reads an ASCII decimal integer.
-
readInt(String)
- Reads an ASCII decimal integer, after displaying given prompt.
-
readLine()
- Reads in a line that has been terminated by a \n, \r,
\r\n or EOF.
-
readLine(String)
- Reads in a line that has been terminated by a \n, \r,
\r\n or EOF, after displaying the given prompt.
-
readLong()
- Reads an ASCII decimal long.
-
readLong(String)
- Reads an ASCII decimal long, after displaying the given prompt.
-
readShort()
- Reads an ASCII decimal short.
-
readShort(String)
- Reads an ASCII decimal short, after displaying given prompt.
-
readWord()
- Reads a word.
-
readWord(String)
- Reads a word, after displaying given prompt.
Console
public Console()
main
public static void main(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.
printPrompt
protected static void printPrompt(String prompt)
- Displays standard out a string prompt followed by a space
- Parameters:
- prompt - string displayed on standard out
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(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(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(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(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(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(String format,
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(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(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()
- 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
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.
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 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.
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.
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(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:
- an optional '-' to make the number negative
- 0 or more digits
- an optional period follows by more digits
- an optional 'e' or 'E' to introduce the exponent
- an optional '-' to make the exponent negative
- digits making up the exponent
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.
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:
- an optional '-' to make the number negative
- 0 or more digits
- an optional period follows by more digits
- an optional 'e' or 'E' to introduce the exponent
- an optional '-' to make the exponent negative
- digits making up the exponent
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.
readFloat
public static float readFloat()
- Reads an ASCII decimal floating point number.
A floating point number is defined as follows:
- an optional '-' to make the number negative
- 0 or more digits
- an optional period follows by more digits
- an optional 'e' or 'E' to introduce the exponent
- an optional '-' to make the exponent negative
- digits making up the exponent
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.
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:
- an optional '-' to make the number negative
- 0 or more digits
- an optional period follows by more digits
- an optional 'e' or 'E' to introduce the exponent
- an optional '-' to make the exponent negative
- digits making up the exponent
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.
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(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 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 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.
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(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:
- ASCIIInputStream@readInt
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
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.
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