public class ITI1120
extends java.lang.Object
Two sets of read methods are provided.
readInt()
, readDouble()
,
readChar()
, readBoolean()
,
readString()
: read one value of the specified type
readIntLine()
,readDoubleLine()
,
readCharLine()
: read all values from a line, create an array of the
proper length, and put the values into the array.
None of these methods allow values of different types to be read from the same line of input.
Note that Integer.parseInt()
does not allow a leading + so,
for example, +123 will be rejected. However, the double
reader
does not require a leading 0 so, for example, -.2 will be accepted.
Modifier and Type | Method and Description |
---|---|
static boolean |
readBoolean()
Read a single
boolean value from the keyboard. |
static char |
readChar()
Read a single
char value from the keyboard. |
static char[] |
readCharLine()
Read an array of
char values from the keyboard. |
static double |
readDouble()
Read a single
double value from the keyboard. |
static double[] |
readDoubleLine()
Read an array of
double values from the keyboard. |
static int |
readInt()
Read a single
int value from the keyboard. |
static int[] |
readIntLine()
Read an array of
int values from the keyboard. |
static java.lang.String |
readString()
Read a String from the keyboard.
|
public static int readInt()
int
value from the keyboard.
If the user enters a non-int
value, a
NumberFormatException
will be thrown, and the program will
terminate. Typing return without entering any value will also produce
this result.
int
containing the value entered from the
keyboard.public static double readDouble()
double
value from the keyboard.
If the user enters a non-double
value, a
NumberFormatException
will be thrown, and the program will
terminate. Typing return without entering any value will also produce
this result.
double
containing the value entered from the
keyboard.public static boolean readBoolean()
boolean
value from the keyboard.
If the user enters any variation of the word "true" in upper, lower, or
mixed case, the result will be the boolean
value
true
. Otherwise, the result will be the
boolean
value false
.
boolean
containing the value entered from the
keyboard.public static char readChar()
char
value from the keyboard.
If the types return without entering any character, a
StringIndexOutOfBoundsException
will be thrown, and the program
will terminate. Otherwise, the first character of what the user typed
will be returned as a char
value.
char
containing the value entered from the
keyboard.public static double[] readDoubleLine()
double
values from the keyboard.
The values must be separated by at least one space as they are entered.
If the user enters a non-double
value, a
NumberFormatException
will be thrown, and the program will
terminate.
If all values are legal double
values, a new array is
created. The length of the array is set by counting the number of values
entered by the user. The values are then entered into the corresponding
array positions.
double
values containing the
set of values entered from the keyboard.public static int[] readIntLine()
int
values from the keyboard.
The values must be separated by at least one space as they are entered.
If the user enters a non-int
value, a
NumberFormatException
will be thrown, and the program will
terminate.
If all values are legal double
values, a new array is
created. The length of the array is set by counting the number of values
entered by the user. The values are then entered into the corresponding
array positions.
double
values containing the
set of values entered from the keyboard.public static char[] readCharLine()
char
values from the keyboard.
After the user has entered a string of characters, hitting 'enter' returns to the calling method. All characters including spaces are put in the array.
The String entered by the user is converted to an array of characters of the appropriate length.
char
values containing the set
of characters entered from the keyboard.public static java.lang.String readString()
String
containing the user's input.