Java provides a Scanner class to facilitate data input/output. In this section, you will learn about the Scanner class that is used to get input from the user. Java also defines various methods from the Scanner class that can convert user input into appropriate data types before conducting any operation on the data.
11. Digits as Input
Answer:
Yes.
Digits as Input
Digits as Input
Collecting characters from the keyboard is done by the operating system. While this is going on, the Java program is suspended. The user can edit the line, and then hit "enter" to signal that it is complete. Only then does the Java program see the characters and resume execution.
Here is another run of the program:
Enter the data:
Columbus sailed in 1492.
You entered:Columbus sailed in 1492.
Notice that the characters '1', '4', '9', and '2' were read in and written out just as were the other characters.
Now consider yet another run:
Enter the data:
1492
You entered:1492
Nothing special here. The '1', '4', '9', and '2' are just characters. A String
object is created, and those characters are stored in the object, just like any characters. If you want the user to enter numeric data, your program must convert from character data to a numeric type.
Question 11:
What primitive type should normally be used to hold integer data?