il.ac.tau.cs.software1.simple_io
Class LineInput
java.lang.Object
|
+--il.ac.tau.cs.software1.simple_io.LineInput
- public class LineInput
- extends java.lang.Object
Class for simple console input. A class designed primarily for simple
keyboard input of the form one input value per line. If the user enters
an improper input, i.e., an input of the wrong type or a blank line,
then the user is prompted to reenter the input and given a brief
explanation of what is required. Also includes some additional methods
to input single numbers, words, and characters, without going to the
next line.
Method Summary |
static int |
read()
Reads
the first byte in the input stream and returns that byte as an int. |
static byte |
readByte()
Reads
the first string of nonwhite characters and returns the byte value it
represents. |
static char |
readChar()
Reads
the next input character and returns that character. |
static double |
readDouble()
Reads
the first string of nonwhite characters and returns the double value it
represents. |
static float |
readFloat()
Reads
the first string of nonwhite characters and returns the float value it
represents. |
static int |
readInt()
Reads
the first string of nonwhite characters and returns the int value it
represents. |
static java.lang.String |
readLine()
Reads
a line of text and returns that line as a String value. |
static java.lang.String |
readLine(java.lang.String msg)
Reads
a line of text and returns that line as a String value. |
static boolean |
readLineBoolean()
Returns
a boolean value according to the input string. |
static boolean |
readLineBoolean(java.lang.String msg)
Returns
a boolean value according to the input string. |
static byte |
readLineByte()
Reads
and returns a number as a value of type byte. |
static double |
readLineDouble()
Reads
and returns a number as a value of type double. |
static double |
readLineDouble(java.lang.String msg)
Reads
and returns a number as a value of type double. |
static float |
readLineFloat()
Reads
and returns a number as a value of type float. |
static float |
readLineFloat(java.lang.String msg)
Reads
and returns a number as a value of type float. |
static int |
readLineInt()
Reads
and returns a number as a value of type int. |
static int |
readLineInt(java.lang.String msg)
Reads
and returns a number as a value of type int. |
static long |
readLineLong()
Reads
and returns a number as a value of type long. |
static long |
readLineLong(java.lang.String msg)
Reads
and returns a number as a value of type long. |
static char |
readLineNonwhiteChar()
Reads
the first nonwhite character on a line and returns that character. |
static char |
readLineNonwhiteChar(java.lang.String msg)
Reads
the first nonwhite character on a line and returns that character. |
static short |
readLineShort()
Reads
and returns the number as a value of type short. |
static java.lang.String |
readLineWord()
Reads
the first string of nonwhite characters on a line and returns that
string. |
static java.lang.String |
readLineWord(java.lang.String msg)
Reads
the first string of nonwhite characters on a line and returns that
string. |
static long |
readLong()
Reads
the first string of nonwhite characters and returns the long value it
represents. |
static char |
readNonwhiteChar()
Reads
the next nonwhite input character and returns that character. |
static short |
readShort()
Reads
the first string of nonwhite characters and returns the short value it
represents. |
static java.lang.String |
readWord()
Reads
the first string of nonwhite characters and returns that string. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass,
hashCode, notify, notifyAll, toString, wait, wait, wait |
LineInput
public LineInput()
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'. (Almost 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 read as a String value.
readLine
public static java.lang.String readLine(java.lang.String msg)
- Reads a line of text and returns that line as a String
value.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The line read as a String value.
- See Also:
readLine()
readLineWord
public static java.lang.String readLineWord()
- Reads the first string of nonwhite characters on a line and
returns that string. The rest of the line is discarded. If the line
contains only white space, then the user is asked to reenter the line.
-
- Returns:
- The first String of nonwhite characters on a line.
readLineWord
public static java.lang.String readLineWord(java.lang.String msg)
- Reads the first string of nonwhite characters on a line and
returns that string. The rest of the line is discarded. If the line
contains only white space, then the user is asked to reenter the line.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The first String of nonwhite characters on a line.
- See Also:
readLineWord()
readLineInt
public static int readLineInt()
- Reads and returns a number as a value of type int.
Precondition: The user has entered a number of type int on a line by
itself, except that there may be white space before and/or after the
number. 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, this applies to incorrect number formats and
blank lines.
-
- Returns:
- The number read as int value.
readLineInt
public static int readLineInt(java.lang.String msg)
- Reads and returns a number as a value of type int. The rest
of the line is discarded.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The number read as int value.
- See Also:
readLineInt()
readLineLong
public static long readLineLong()
- Reads and returns a number as a value of type long.
Precondition: The user has entered a number of type long on a line by
itself, except that there may be white space before and/or after the
number. 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, this applies to incorrect number formats and
blank lines.
-
- Returns:
- The number read as long value.
readLineLong
public static long readLineLong(java.lang.String msg)
- Reads and returns a number as a value of type long. The
rest of the line is discarded.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The number read as long value.
- See Also:
readLineLong()
readLineDouble
public static double readLineDouble()
- Reads and returns a number as a value of type double.
Precondition: The user has entered a number of type double on a line by
itself, except that there may be white space before and/or after the
number. 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, this applies to incorrect number formats and
blank lines.
-
- Returns:
- The number read as double value.
readLineDouble
public static double readLineDouble(java.lang.String msg)
- Reads and returns a number as a value of type double. The
rest of the line is discarded. If the input is
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The number read as double value.
- See Also:
readLineDouble()
readLineFloat
public static float readLineFloat()
- Reads and returns a number as a value of type float.
Precondition: The user has entered a number of type float on a line by
itself, except that there may be white space before and/or after the
number. 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, this applies to incorrect number formats and
blank lines.
-
- Returns:
- The number read as float value.
readLineFloat
public static float readLineFloat(java.lang.String msg)
- Reads and returns a number as a value of type float. The
rest of the line is discarded.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The number read as float value.
- See Also:
readLineFloat()
readLineNonwhiteChar
public static char readLineNonwhiteChar()
- Reads the first nonwhite character on a line and returns
that character. The rest of the line is discarded. If the line contains
only white space, then the user is asked to reenter the line.
-
- Returns:
- The first nonwhite character.
readLineNonwhiteChar
public static char readLineNonwhiteChar(java.lang.String msg)
- Reads the first nonwhite character on a line and returns
that character. The rest of the line is discarded. If the line contains
only white space, then the user is asked to reenter the line.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- The first nonwhite character.
- See Also:
readLineNonwhiteChar()
readLineBoolean
public static boolean readLineBoolean()
- Returns a boolean value according to the input string.
Input should consist of a single word on a line, possibly surrounded by
white space. 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 (e.g., multiple
words or different words), then the user is asked to reenter the input.
-
- Returns:
- A boolean value according to the input string.
readLineBoolean
public static boolean readLineBoolean(java.lang.String msg)
- Returns a boolean value according to the input string. If
the input word is "true" or "t", then true is returned. If the input
word is "false" or "f", then false is returned.
-
- Parameters:
msg
- The request message to
show to the user.
- Returns:
- A boolean value according to the input string.
- See Also:
readLineBoolean()
readChar
public static char readChar()
- Reads the next input character and returns that character.
The next read takes place on the same line where this one left off.
-
- Returns:
- The next input character.
readNonwhiteChar
public static char readNonwhiteChar()
- Reads the next nonwhite input character and returns that
character. The next read takes place immediately after the character
read.
-
- Returns:
- The next nonwhite input character.
readInt
public static int readInt()
throws java.lang.NumberFormatException
- Reads the first string of nonwhite characters and returns
the int value it represents. Precondition: The next input in the stream
consists of an int value, possibly preceded by white space, but
definitely followed by white space. The reading discards the first
whitespace character after the word. The next read takes place
immediately after the discarded whitespace. In particular, if the word
is at the end of a line, the next reading will take place starting on
the next line.
-
- Returns:
- The int value of the first string of nonwhite
characters.
- Throws:
java.lang.NumberFormatException
- is thrown if the next word does not represent an int value
readLong
public static long readLong()
throws java.lang.NumberFormatException
- Reads the first string of nonwhite characters and returns
the long value it represents. Precondition: The next input in the
stream consists of an long value, possibly preceded by white space, but
definitely followed by white space. The reading discards the first
whitespace character after the word. The next read takes place
immediately after the discarded whitespace. In particular, if the word
is at the end of a line, the next reading will take place starting on
the next line.
-
- Returns:
- The long value of the first string of nonwhite
characters.
- Throws:
java.lang.NumberFormatException
- is thrown if the next word does not represent a long value
readDouble
public static double readDouble()
throws java.lang.NumberFormatException
- Reads the first string of nonwhite characters and returns
the double value it represents. Precondition: The next input in the
stream consists of an double value, possibly preceded by white space,
but definitely followed by white space. The reading discards the first
whitespace character after the word. The next read takes place
immediately after the discarded whitespace. In particular, if the word
is at the end of a line, the next reading will take place starting on
the next line.
-
- Returns:
- The double value of the first string of nonwhite
characters.
- Throws:
java.lang.NumberFormatException
- is thrown if the next word does not represent a double value
readFloat
public static float readFloat()
throws java.lang.NumberFormatException
- Reads the first string of nonwhite characters and returns
the float value it represents. Precondition: The next input in the
stream consists of an float value, possibly preceded by white space,
but definitely followed by white space. The reading discards the first
whitespace character after the word. The next read takes place
immediately after the discarded whitespace. In particular, if the word
is at the end of a line, the next reading will take place starting on
the next line.
-
- Returns:
- The float value of the first string of nonwhite
characters.
- Throws:
java.lang.NumberFormatException
- is thrown if the next word does not represent a float value
readWord
public static java.lang.String readWord()
- Reads the first string of nonwhite characters and returns
that string. Discards the first whitespace character after the string
read. The next read takes place immediately after the discarded
whitespace. In particular, if the string read is at the end of a line,
the next reading will take place starting on the next line. Note, that
if it receives blank lines, it will wait until it gets a nonwhitespace
character.
-
- Returns:
- the first String of nonwhite characters.
readLineByte
public static byte readLineByte()
- Reads and returns a number as a value of type byte.
Precondition: The user has entered a number of type byte on a line by
itself, except that there may be white space before and/or after the
number. 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, this applies to incorrect number formats and
blank lines.
-
- Returns:
- The number read as double value.
readLineShort
public static short readLineShort()
- Reads and returns the number as a value of type short.
Precondition: The user has entered a number of type short on a line by
itself, except that there may be white space before and/or after the
number. 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, this applies to incorrect number formats and
blank lines.
-
- Returns:
- The number read as double value.
readByte
public static byte readByte()
throws java.lang.NumberFormatException
- Reads the first string of nonwhite characters and returns
the byte value it represents. Precondition: The next input in the
stream consists of an byte value, possibly preceded by white space, but
definitely followed by white space. The reading discards the first
whitespace character after the word. The next read takes place
immediately after the discarded whitespace. In particular, if the word
is at the end of a line, the next reading will take place starting on
the next line.
-
- Returns:
- The byte value of the first string of nonwhite
characters.
- Throws:
java.lang.NumberFormatException
- is thrown if the next word does not represent a byte value
readShort
public static short readShort()
throws java.lang.NumberFormatException
- Reads the first string of nonwhite characters and returns
the short value it represents. Precondition: The next input in the
stream consists of an short value, possibly preceded by white space,
but definitely followed by white space. The reading discards the first
whitespace character after the word. The next read takes place
immediately after the discarded whitespace. In particular, if the word
is at the end of a line, the next reading will take place starting on
the next line.
-
- Returns:
- The short value of the first string of nonwhite
characters.
- Throws:
java.lang.NumberFormatException
- is thrown if the next word does not represent a short value
read
public static int read()
- Reads the first byte in the input stream and returns that
byte as an int. The next read takes place where this one left off. This
read is the same as System.in.read(), except that it catches
IOExceptions.
-
- Returns:
- The first byte in the input stream as an int.