3.10 Input from the Console Using the Scanner Class
As our programs become more complex, we will need to allow the users of our programs to input data. User input can be read into our program in several ways:
from the keyboard
from a file
through a Graphical User Interface (GUI)
The Java Class Library provides classes for all types of data input. In this chapter, we will concentrate on inputting data from the keyboard.
The Scanner class provides methods for reading byte, short, int, long, float, double, boolean, and String data types from the keyboard. These methods are shown in Table 3.8.
TABLE 3.8 Selected Methods of the Scanner Class
A Scanner Class Constructor |
|
---|---|
Scanner( InputStream dataSource ) creates a Scanner object ... |
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.