Java Scanner : taking user input

The Scanner class is used in Java to read input from various sources, including the keyboard, files, and network sockets.

Here’s an example of how the Scanner class can be used to read information about cars:

In this example, a Car class is defined with four fields: make, model, year, and speed. The main method creates a Scanner object to read input from the keyboard, and then prompts the user to enter information about a car. The information is read using the nextLine and nextInt methods of the Scanner class and used to create a Car object. Finally, the Car object is printed to the console.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top