Class ConsoleIn
java.lang.Object
|
+--ConsoleIn
- public class ConsoleIn
- extends java.lang.Object
Class for robust keyboard input.
If the user enters an improper input, that is, an input of the wrong type
or a blank line when the line should be nonblank, then
the user is given an error message and is prompted to reenter the input
|
Method Summary |
static java.lang.String |
readLine()
Reads a line of text and returns that line as a String
value. |
static boolean |
readLineBoolean()
Input should consist of a single word on a line, possibly
surrounded by whitespace. |
static byte |
readLineByte()
The user is supposed to enter a whole number of type byte
on a line by itself. |
static double |
readLineDouble()
The user is supposed to enter a number of type double
on a line by itself. |
static float |
readLineFloat()
The user is supposed to enter a number of type float
on a line by itself. |
static int |
readLineInt()
The user is supposed to enter a whole number of type int
on a line by itself. |
static long |
readLineLong()
The user is supposed to enter a whole number of type long
on a line by itself. |
static char |
readLineNonwhiteChar()
Returns the first non-whitespace character on the input line
The rest of the input line is discarded. |
static short |
readLineShort()
The user is supposed to enter a whole number of type short
on a line by itself. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ConsoleIn
public ConsoleIn()
readLine
public static java.lang.String readLine()
- Reads a line of text and returns that line as a String
value. The end of a line must be indicated either by a
new-line character '\n' or by a carriage return '\r'
followed by a new-line character '\n'. (Essentially all systems
do this automatically. So you need not worry about this
detail.) Neither the '\n', nor the '\r' if present, are
part of the string returned. This will read the rest of a
line if the line is already partially read.
- Returns:
- The line input from the keyboard.
readLineInt
public static int readLineInt()
- The user is supposed to enter a whole number of type int
on a line by itself. There may be whitespace before
and/or after the number.
Returns the number entered as a value of type int.
The rest of the line is discarded.If the input is not
entered correctly, then in most cases, the user will be asked
to reenter the input. In particular ,incorrect number formats
and blank lines result in a prompt to reenter the input.
- Returns:
- Returns the integer input.
readLineLong
public static long readLineLong()
- The user is supposed to enter a whole number of type long
on a line by itself. There may be whitespace before
and/or after the number.
Returns the number entered as a value of type long.
The rest of the line is discarded.If the input is not
entered correctly, then in most cases, the user will be asked
to reenter the input. In particular, incorrect number formats
and blank lines result in a prompt to reenter the input.
- Returns:
- Returns the integer input.
readLineByte
public static byte readLineByte()
- The user is supposed to enter a whole number of type byte
on a line by itself. There may be whitespace before
and/or after the number.
Returns the number entered as a value of type byte.
The rest of the line is discarded.If the input is not
entered correctly, then in most cases, the user will be asked
to reenter the input. In particular, incorrect number formats
and blank lines result in a prompt to reenter the input.
- Returns:
- Returns the integer input.
readLineShort
public static short readLineShort()
- The user is supposed to enter a whole number of type short
on a line by itself. There may be whitespace before
and/or after the number.
Returns the number entered as a value of type short.
The rest of the line is discarded.If the input is not
entered correctly, then in most cases, the user will be asked
to reenter the input. In particular, incorrect number formats
and blank lines result in a prompt to reenter the input.
- Returns:
- Returns the integer input.
readLineDouble
public static double readLineDouble()
- The user is supposed to enter a number of type double
on a line by itself. There may be whitespace before
and/or after the number.
Returns the number entered as a value of type double.
The rest of the line is discarded.If the input is not
entered correctly, then in most cases, the user will be asked
to reenter the input. In particular, incorrect number formats
and blank lines result in a prompt to reenter the input.
- Returns:
- The floating point number input.
readLineFloat
public static float readLineFloat()
- The user is supposed to enter a number of type float
on a line by itself. There may be whitespace before
and/or after the number.
Returns the number entered as a value of type float.
The rest of the line is discarded.If the input is not
entered correctly, then in most cases, the user will be asked
to reenter the input. In particular, incorrect number formats
and blank lines result in a prompt to reenter the input.
- Returns:
- The floating point number input.
readLineNonwhiteChar
public static char readLineNonwhiteChar()
- Returns the first non-whitespace character on the input line
The rest of the input line is discarded. If the line contains
only whitespace, the user is asked to reenter the line.
- Returns:
- The first non-whitespace character on the input line.
readLineBoolean
public static boolean readLineBoolean()
- Input should consist of a single word on a line, possibly
surrounded by whitespace. The line is read and discarded.
If the input word is "true" or "t", then true is returned.
If the input word is "false" or "f", then false is
returned. Uppercase and lowercase letters are considered
equal. If the user enters anything else (for example, multiple
words or different words), the user is asked to reenter the input.
- Returns:
- The boolean value entered.