Java Reference
In-Depth Information
d. The expression:
console.nextLine()
retrieves the next input as a string until the end of the line; that is, the
value of this expression is the next input line. (Note that this expression
also reads the newline character, but the newline character is not stored
as part of the string.)
2
While scanning for the next input, the expressions console.nextInt() ,
console.nextDouble() , and console.next() skip whitespace characters. Whitespace
characters are blanks and certain nonprintable characters, such as newline and tab.
System.in is called a standard input stream object and is designed to input data from the
standard input device. However, the object System.in extracts data in the form of
bytes from the input stream. Therefore, using System.in , we first create a Scanner
object, such as console , as shown previously, so that the data can be extracted in a
desired form. (The meaning of the word new is explained in Chapter 3.)
The class Scanner is added to the Java library in Java version 5.0. Therefore, this
class is not available in Java versions lower than 5.0.
EXAMPLE 2-15
Suppose that miles is a variable of type double . Further suppose that the input is 73.65 .
Consider the following statements:
miles = console.nextDouble();
This statement causes the computer to get the input, which is 73.65 , from the standard
input device, and stores it in the variable miles . That is, after the execution of this
statement, the value of the variable miles is 73.65 .
Example 2-16 further explains how to input numeric data into a program.
EXAMPLE 2-16
Suppose we have the following declaration:
static Scanner console = new Scanner(System.in);
Consider the following statements:
int feet;
int inches;
 
Search WWH ::




Custom Search