CHAPTER 2 Data Using the Scanner Class for Keyboard Input Using the Scanner Clas
ID: 3700885 • Letter: C
Question
CHAPTER 2 Data Using the Scanner Class for Keyboard Input Using the Scanner Class for Keyboard Input In Chapter 1, you learmed how to display output on the monitor using the System.out property. System.out refers to the standard output device, which usually is the monitor Frequently you also want to create interactive programs that accept input from a user. To Method nextDoubleO hextIntO Retrieves input as a double Retrieves input as an int 76 do so, you can use System.in, which refers to the standard input device (normally the hextlineO the next line of data and returns it as a String keyboard). the ing ou can use the printO and printInO methods to display many data types for example, Table 2-8 Selected Scanner class can use them to display a double, int, or String. The Systen.in object is not as flexible t is designed to read only bytes. Thats a problem, because you often want to accept data of r types. Fortunately, the designers of Java have created a class named Scanner that makes Ih addition to the methods listed in Table 28, the Scanner class contains methods caled nextByteO nextFloatO, nextlongO, and nextShortO that work as you would expect based on their identifiers ysten.in more flexible. T'o create a Scanner object and connect it to the System.in object, you write a statement imilar to the following: r inputDevice-new Scanner(System.in) The keybosrd, you can use the nextLineO method and then use the charAtO method. The chapter Characters, Strings, and the StringBuilder provides more details about the charAtO method class does not contain a nextCharO method. To retrieve a single character from the The portion of the statement to the left of the assignment operator, Scanner inputDevice, declares an object of type Scanner with the programmer-chosen name inputDevice, in exactly the same way that int x; declares an integer with the programmer chosen name x. The portion of the statement to the right of the assignment operator, new Scanner (Systen.in), creates a Scanner object that is connected to the System.in property. In other words, the created Scanner object is connected to the default input device. The keyword new is required by Java; you will use it whenever you create objects that are more complex than the simple data types. Figure 2-10 contains a program that uses two of the Scanner class methods. The program reads a string and an integer from the keyboard and displays them. The Scanner class is used in the four shaded statements in the figure. The first shaded statement is import java.util.Scanner:. This statement imports the In the chapter More Object Concepts you will learn that the second part of the Scanner decaration cals a special method caled a constructor that is part of the prewritten Scanner class. You asowl learn more about the Java keyword new in the next two chapters. package necessary to use the Scanner dlass. The second shaded statement declares a Scanner object named inputDevice. . The third shaded statement uses the nextLineO method to retrieve a line of text from the kcyboard and store it in the nane variable. The last shaded statement uses the nextIntO method to retrieve an integer from the keyboard and store it in the age variable. The assignment operator in the Scanner declaration statement assigns the value of the new object- that is, its memory address-to the inputDevice object in the program A Scanner object breaks its input into units called tokens, separating them when it encounters whitespace. The resulting tokens can then be converted into values of disferent types using the various class methods. Table 2-8 summarizes some of the most useful methods that read different data types from the default input device. Each retrieves a value from the keyboard and returns it as the appropriate data type. The appropriate method executes, retrieving data when a user presses an appropriate whitespace key at the keyboard. The nextineO method does not retrieve data until the user presses Enter; the other methods retrieve data when the user presses Enter, the spacebar, or the tab key. Figure 2-11 shows a typical execution of the program. ava programmers would say that the Scanner methods return the appropriate value. That also means that the value of the method is the appropriate value, and that you can assign the returned value to a variable, dsplay or use i in other legal statements. In the chapter Using Methods, Clusses, and Objects, you will earn how to write your own methods that return values.Explanation / Answer
Scanner is a class in Java.
Talking in terms of its use: it takes in or you can say it reads the input from the keyboard.
When we are doing desktop based console programming its object reads the characters referred as token as a byte/int/short/float etc types.
let us take a simple example:
import java.util.Scanner;
class Test{
public static void main(String args[]){
Scanner in=new Scanner(System.in);// in is the name of the object
System.out.println("Enter your name"); //On console it asks you to enter your name
String name=in.next();//in.next() will take input as string
System.out.println("Your name is:"+name);// Your name will be displayed
in.close();
}}
Output:
Enter your name
ABCD
Your name is ABCD
On page 77, there are various methods, these methods are based on the datatype.
For Example: take age, this will be a number, so nextInt() method can be used to read age.
On page 78, in the example, they have shown how various datatypes are taken in using scanner object.
Now, regarding the echoing the input,it is nothing but system.out.println, ie. we are explicitly getting the entered input through scanner class to get printed on screen.
Page 79 explaination:
Regarding next(),nextInt() and nextDouble() method, reads the next token that is in the buffer till a blank space or enter key is pressed.
However,nextLine() method moves the scanner position to the next line and returns the value as a string.
So, if you observe,in the example, on page 79,after the name prompt, input.nextLine() is used and so scanner is moved to the next line and the next line is returned.
This happens when numeric scanner or next() is used before nextLine().
The best use of nextLine():
Scanner objects next(),nextInt(), etc. will not introduce a new line. But if we use nextLine() we can have the feature of enterkey and will introduce.
So, in the eg. on page 80, inputdevice.nextline(); is consuming the enter key that comes after th