Java Reference
In-Depth Information
Suppose the input is:
23 7
Next, consider the following statements:
feet = console.nextInt();
//Line 1
inches = console.nextInt();
//Line 2
The statement in Line 1 stores the number 23 into the variable feet . The statement in
Line 2 stores the number 7 into the variable inches . Notice that when these numbers are
entered at the keyboard, they are separated with a blank. In fact, they can be separated
with one or more blanks, lines, or even the tab character. (Note that we have numbered
the statements as Line 1 and Line 2, so that we can conveniently refer to a particular
statement and explain its meaning.)
The following Java program shows the effect of the preceding input statements:
// This program illustrates how input statements work.
import java.util.*;
public class Example2_16
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int feet;
int inches;
System.out.println("Enter two integers separated by spaces.");
feet = console.nextInt();
inches = console.nextInt();
System.out.println("feet = " + feet);
System.out.println("inches = " + inches);
}
}
Sample Run: (In this sample run, the user input is shaded.)
Enter two integers separated by spaces.
23 7
feet = 23
inches = 7
In the preceding program, notice the first line:
import java.util.*;
This line is required to use the class Scanner .
 
Search WWH ::




Custom Search