Taken together, the lines import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
int inputNumber = keyboard.nextInt();
in Listing 5-1 get whatever number the user types on the computer’s keyboard.
FIGURE 5-1:
Two runs of the guessing game.
word-for-word whenever you want to read from the keyboard. Include the first two lines (the import and Scanner lines) just once in your program. Later in your program, wherever the user types an int value, include a line with a call to nextInt (as in the last of the preceding three lines of code).
Of all the names in these three lines of code, the only two names I coined myself are inputNumber and keyboard. All the other names are part of Java. So, if I want to be creative, I can write the lines this way:
import java.util.Scanner;
Scanner readingThingie = new Scanner(System.in);
int valueTypedIn = readingThingie.nextInt();
I can also beef up my program’s import declarations, as I do later on, in Listings 5-2 and 5-3. Other than that, I have very little leeway.
As you read on in this book, you’ll start recognizing the patterns behind these three lines of code, so I don’t clutter up this section with all the details. For now, you can just copy these three lines and keep the following guidelines in mind:
»
When you import java.util.Scanner, you don’t use the word static.But importing Scanner is different from importing System.out. When you import java.lang.System.out, you use the word static. (Refer to Listing 5-1.) The difference creeps into the code because Scanner is the name of a class and System.out isn’t the name of a class.
For a quick look at the use of the word static in import declarations, see the sidebar in Chapter 4 about import declarations: the ugly truth. For a more complete story about the word, see Chapter 10.
»
Typically (on a desktop or laptop computer), the name System.in stands for the keyboard.To get characters from someplace other than the keyboard, you can type something other than System.in inside the parentheses.
What else can you put inside the new Scanner(...) parentheses? For some ideas, see Chapter 8.
In Listing 5-1, I make the arbitrary decision to give one of my variables the name keyboard. The name keyboard reminds you, the reader, that this variable refers to a bunch of plastic buttons in front of your computer.
Naming something keyboard tells Java nothing about plastic buttons or about user input. On the other hand, the name System.in always tells Java
(System.in) in Listing 5-1 connects the name keyboard with the plastic buttons that we all know and love.
»
When you expect the user to type an int value (a whole number of some kind), use nextInt().If you expect the user to type a double value (a number containing a decimal point), use nextDouble(). If you expect the user to type true or false, use nextBool- ean(). If you expect the user to type a word like Barry, Java, or Hello, use next(). Decimal points vary from one country to another. In the United States, 10.5 (with a period) represents ten-and-a-half, but in France, 10,5 (with a comma) represents ten-and-a-half. In Switzerland, 10.50 is an amount of money, and 10,50 is an amount that’s not money-related. In the Persian language, a decimal point looks like a slash (but it sits a bit lower than the digit characters).
Your computer’s operating system stores information about the country you live in, and Java reads that information to decide what ten-and-a-half looks like. If you run a program containing a nextDouble() method call and Java responds with an InputMismatchException, check your input. You might have input 10.5 when your country’s conventions require 10,5 (or another way of representing ten-and-a-half). For more information, see the sidebar “Where on earth do you live?” in Chapter 8.
For an example in which the user types a word, see Listing 5-3, later in this chapter. For an example in which the user types a single character, see Listing 6-4, in Chapter 6. For an example in which a program reads an entire line of text (all in one big gulp), see Chapter 8.
»
You can get several values from the keyboard, one after another.To do this, use the keyboard.nextInt() code several times.
To see a program that reads more than one value from the keyboard, go to Listing 5-4, later in this chapter.
»
Whenever you use Java’s Scanner, you should call the close method after your last nextInt call (or your last nextDouble call or your last nextWhatever call).In Listing 5-1, the main method’s last statement is keyboard.close();
This statement does some housekeeping to disconnect the Java program from the computer keyboard. (The amount of required housekeeping is more than you might think!) If I omit this statement from Listing 5-1, nothing terrible happens. Java’s virtual machine usually cleans up after itself very nicely. But using close() to explicitly detach from the keyboard is good practice, and some IDEs display warnings if you omit the keyboard.close() statement. In
In Chapter 13, I show you a more reliable way to incorporate close() in your Java program.
When your program calls System.out.println, your program uses the computer’s screen. So why don’t you call a close method after all your System.out.println calls? The answer is subtle. In Listing 5-1, your own code connects to the keyboard by calling new Scanner(System.in). So, later in the program, your code cleans up after itself by calling the close method.
But with System.out.println, your own code doesn’t create a connection to the screen. (The out variable refers to a PrintStream, but you don’t call new PrintStream() to prepare for calling System.out.println.) Instead, the Java virtual machine connects to the screen on your behalf. The Java virtual machine’s code (which you never have to see) contains a call to new PrintStream() in preparation for your calling System.out.println. So, because it’s a well-behaved piece of code, the Java virtual machine eventually calls out.close() with no effort on your part.