System.out.println(whatever text you want displayed );
What she didn’t tell you was how to fetch characters from the computer keyboard. There are lots of ways to do it, but the one I recommend in this chapter is
keyboard.nextLine()
Now, here’s the fun part. Calling the nextLine method doesn’t just scoop characters from the keyboard. When the computer runs your program, the computer substitutes whatever you type on the keyboard in place of the text keyboard.nextLine() .
To understand this, look at the statement in Listing 5-1 :
System.out.println(keyboard.nextLine());
When you run the program, the computer sees your call to nextLine and stops dead in its tracks. (Refer to Figure 5-2 .) The computer waits for you to type a line of text. So (refer to Figure 5-3 ) you type this line:
Hey, there’s an echo in here.
The computer substitutes this entire Hey line for the
keyboard.nextLine() call in your program. The process is illustrated in Figure 5-5 .
image
FIGURE 5-5: The computer substitutes text in place of the nextLine call.
The call to keyboard.nextLine() is nestled inside the
System.out.println call. So, when all is said and done, the computer behaves as though the statement in Listing 5-1 looks like this:
System.out.println("Hey, there’s an echo in here.");
The computer displays another copy of the text Hey, there’s an echo in here. on the screen. That’s why you see two copies of the Hey line in Figure 5-4 .
Getting numbers, words, and other things
In Listing 5-1 , the words keyboard.nextLine() get an entire line of text from the computer keyboard. If you type
Testing 1 2 3
the program in Listing 5-1 echoes back the entire Testing 1 2 3 line of text.
Sometimes you don’t want a program to get an entire line of text.
Instead, you want the program to get a piece of a line. For example, when you type 1 2 3 , you may want the computer to get the number 1.
(Maybe the number 1 stands for one customer or something like that.) In such situations, you don’t put keyboard.nextLine() in your program.
Instead, you use keyboard. nextInt() .
Table 5-1 shows you a few variations on the keyboard.next business.
Unfortunately, the table’s entries aren’t very predictable. To read a line of input, you call nextLine . But to read a word of input, you don’t call
nextWord . (The Java API has no nextWord method.) Instead, to read a word, you call next .
TABLE 5-1 Some Scanner Methods
To Read This … … Make This Method Call
A number with no decimal point in it nextInt()
A number with a decimal point in it nextDouble()
A word (ending in a blank space, for example) next()
A line (or what remains of a line after you’ve already
read some data from the line) nextLine()
A single character (such as a letter, digit, or
punctuation character) findWithinHorizon(".",0).charAt(0)
Also, the table’s story has a surprise ending. To read a single character, you don’t call next Something . Instead, you can call the bizarre
findWithinHorizon(".",0).charAt(0 ) combination of methods.
(You’ll have to excuse the folks who created the Scanner class. They created Scanner from a specialized point of view.)
To see some of the table’s methods in action, check other program listings in this book. Chapters 6 , 7 , and 8 have some particularly nice examples.
GETTING SINGLE WORDS AND ENTIRE LINES OF TEXT
Follow the instructions in this chapter’s “Typing and running a program
” section, but make these two changes:
In Step 6, type GetInput in the Name field of the New Java Class dialog box.
Instead of typing the code in Listing 5-1 , type the following program:
import java.util.Scanner;
public class GetInput {
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);
System.out.println(keyboard.next());
System.out.println(keyboard.next());
System.out.println(keyboard.nextLine());
keyboard.close();
} }
When the program runs, type the following text (all on one line) in Eclipse’s Console view, and then press Enter. How does the computer respond? Why?
I enjoy learning Java.
Type three lines of code and don’t look back
Buried innocently inside Listing 5-1 are three extra lines of code. These lines help the computer read input from the keyboard. The three lines are
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
keyboard.close();
Concerning these three lines, I have bad news and good news:
The bad news is, the reasoning behind these lines is difficult to understand. That’s especially true here in Chapter 5 , where I introduce Java’s most fundamental concepts.
The good news is, you don’t have to understand the reasoning behind these three lines. You can copy and paste these lines into any program that gets input from the keyboard. You don’t have to change the lines in any way. These lines work without any modifications in all kinds of Java programs.
Just be sure to put these lines in the right places:
Make the import java.util.Scanner line the first line in your program.
Put the Scanner keyboard = new Scanner(System.in) line inside the main method immediately after the public static void
main(String args[]) { line.
Make the keyboard.close() line the last line in your program.
At some point in the future, you may have to be more careful about the positioning of these three lines. But for now, the rules I give will serve you well.