Inputting Values from the User Another way to assign a value to a variable is to ask the user for input. The advantage to user input is that the program is more flexible. If using hard-coded values and the data values need to change, the code must be edited and recom- piled before the program is run again. With user input, the program can be run multiple times, and the user can enter different values each time. To input values from the user, the Scanner class is used from the Java Class Library. The Java Class Library is a collection of prewritten code that program- mers use repeatedly. It is better to write these code elements once and make them available to programmers whenever they need them. This library is divided into packages that keep similar classes together. In order to use this code, program- mers simply tell the compiler to look in the library for it. The Scanner class is explored in detail in Chapter 6. For now, simply follow three steps to get input. 1. Import the Scanner class. 2. Create a Scanner object. 3. Ask the user for input, and read the inputted value into a variable. The first step is to import the Scanner class using the following statement. This statement is placed after the identification block comment and before the definition of the class name. import java.util.Scanner The compiler needs to know where the Scanner class is defined. The import statement tells the compiler where to find it in the Java Class Library. In the above example, the import statement specifies the Scanner class is in the package java.util. All classes in the Java Class Library are downloaded with the Java JDK. The second step is to create a Scanner object to use for input. Put this state- ment inside the main method: Scanner input = new Scanner( System.in ) The System.in reference means the keyboard. So, this statement creates a Scanner object named input that will be used to input the data the user enters with the keyboard. The third step is to ask the user for the input and read the input into a vari- able. This is done for each data item to input. To ask the user for input, a message called a prompt is outputted. A prompt is a message to the user indicating what the user needs to input or what action to take. To read the user input, call one of the Scanner methods shown in Figure 4-7. To call means to use a method. To call these methods, prefix the method name with the name of the Scanner object created (input) and a period or dot (.). The empty parentheses after the method name are required. The Scanner class provides the two methods in Figure 4-7 for reading integers and real numbers. The Scanner class also has methods for reading the other primi- tive data types, except for chars. For example, suppose a program calculates the area of a circle based on a radius the user enters. After importing Scanner and creating a Scanner object, these statements could be used: System.out.println( "Enter the radius of the circle:" ) // prompt double radius = input.nextDouble( ) // read value When this code runs, the prompt is displayed in the output window. Then, the program pauses and waits for the user to input a value and press the [Enter] key. Goodheart-Willcox Publisher Figure 4-7. Two of the methods available with the Scanner class. Method Method Explanation Explanation nextInt() Read an integer from the user nextDouble() Read a real number from the user Copyright Goodheart-Willcox Co., Inc. Chapter 4 Variables 87