Java Reference
In-Depth Information
We change our program segment to prompt the user for keyboard input and,
as an example, print the square of the integer that was typed:
// Prompt the user for an integer, store it in s , and print its square
System.out.println("Please type an integer");
s= JLiveRead.readLineInt();
System.out.println(" square is " + (s*s));
Reading values of other primitive types
Class JLiveRead contains methods for reading a line of input that contains
values of other primitive types. In each case, the item read may be preceded and
followed by blank characters. There is also a method for reading the whole line
as a String . The methods are:
readLineLong() : Read and return a long value.
readLineFloat() : Read and return a float value.
readLineDouble() : Read and return a double value.
readLineNonwhiteChar() : Read and return the first char value that is not
readLineBoolean() : The input may contain “ t ”, “ true ”, “ f ”, or “ false ”,
using lowercase or uppercase. Read it and return true or false , accordingly.
readLineString() : Read and return a sequence of non-whitespace characters,
as a String .
One more useful method can be used to read in all the typed characters:
readString() : Read and return the typed characters, as a String . The final
carriage return or line feed is not included.
Putting more than one value on a line
The methods discussed above force the user to type exactly one value before
hitting the return/enter key. For example, function readLineInt will complain
and ask you to type an integer again if you type the following before hitting the
return/enter key:
25 4.0 3
Class JLiveRead contains methods that read a single value from a line, per-
haps preceded by whitespace, leaving the rest of the line to be read later. These
methods are:
Search WWH ::

Custom Search