Input and Output

Learn how to handle user inputs in a more efficient way using the Scanner class in this lesson.

Evolution of Scanner class

We have seen before how to take input from the users. How to use the Scanner class is not completely new to us. However, we need to further investigate this matter for one reason.

Our goal is to create software that is interactive. In that regard, the Scanner class is the latest development that helps us write less code. Before that, we used InputStreamReader. It reads bytes and turns them to characters. The read() method of InputStreamReader invokes the reading of bytes from the underlying byte-input stream. We increase the efficiency of InputStreamReader by wrapping the new InputStreamReader object within a BufferedReader object. The constructor of InputStreamReader passes one object that reads the input from the keyboard.

Consequently, the process becomes complex. To avoid those complexities, Java has introduced the Scanner class through which we can easily take the user’s input.

Coding example: 82

The following example shows us those complexities. If you want to rerun the code, write java main in the terminal:

Get hands-on with 1200+ tech skills courses.