Page 125 - Introduction to Programming with Java: A Problem Solving Approach
P. 125
gone just one way—they’ve displayed something on the screen, but they haven’t read any input. With no in- put, our programs have been rather limited. In this section, we’ll discuss how to get input from a user. With input, we’ll be able to write programs that are much more flexible and useful.
Suppose you’re asked to write a program that calculates earnings for a retirement fund.
If there’s no input, your program must make assumptions about contribution amounts, years
before retirement, and so on. Your program then calculates earnings based on those assumptions. Bottom line: Your no-input program calculates earnings for one specific retirement-fund plan. If input is used, your program asks the user to supply contribution amounts, years before retirement, and so forth. Your program then calculates earnings based on those user inputs. So which version of the program is better—the no-input version or the input version? The input version is better because it allows the user to plug in what-if scenar- ios. What happens if I contribute more money? What happens if I postpone retirement until I’m 90?
Input Basics
Sun provides a pre-built class named Scanner, which allows you to get input from either a keyboard or a file. We describe file input in Chapter 15. Prior to that, when we talk about input, you should assume that we’re talking about keyboard input.
The Scanner class is not part of the core Java language. So if you use the Scanner class, you need to tell the compiler where to find it. You do that by importing the Scanner class into your program. More specifically, you need to include this import statement at the top of your program (right after your pro- logue section):
import java.util.Scanner;
Apago PDF Enhancer
We describe import details (like what is java.util?) in Chapter 5. For now, suffice it to say that you need to import the Scanner class in order to prepare your program for input.
There’s one more thing you need to do to prepare your program for input. Insert this statement at the top of your main method:
Scanner stdIn = new Scanner(System.in);
3.23 Input—the Scanner Class 91
Ask: What if?
The new
collection of data. In this case, the object stores characters entered by a user at a keyboard. The stdIn vari- able is a reference variable, and it gets initialized to the address of the newly created Scanner object. After the initialization, the stdIn variable allows you to perform input operations.
With the above overhead in place, you can read and store a line of input by calling the nextLine method like this:
<variable> = stdIn.nextLine();
Let’s put what you’ve learned into practice by using the Scanner class and the nextLine method call in a complete program. See the FriendlyHello program in Figure 3.13. The program prompts the user to enter his/her name, saves the user’s name in a name variable, and then prints a greeting with the user’s name embedded in the greeting.
In the FriendlyHello program, note the “Enter your name: ” print statement. It uses a System.out. print statement rather than a System.out.println statement. Remember what the “ln” in println stands for? It stands for “line.” The System.out.println statement prints a message and then moves the screen’s cursor to the next line. On the other hand, the System.out.print statement prints a message and that’s it. The cursor ends up on the same line as the printed message (just to the right of the last printed character).
Scanner(System.in) expression creates an object. As you now know, an object stores a