Learn how to read input from the user using the Scanner class.
Making programs interactive is a key part of software development, and the simplest way to do this in a console application is by accepting user input. In Java, the primary tool for this is the `Scanner` class, which is part of the `java.util` package. To use it, you first need to import it into your program with `import java.util.Scanner;`. Then, you create an instance of the `Scanner` class, typically attached to the standard input stream, `System.in`. The line `Scanner scanner = new Scanner(System.in);` creates a new scanner object that 'scans' input from the keyboard. Once the `Scanner` object is created, it provides several useful methods to read different types of data. The `nextLine()` method reads a full line of text as a `String`. The `nextInt()` method reads the next token of input as an `int`. Similarly, there are methods like `nextDouble()`, `nextBoolean()`, and so on for other primitive types. It's important to be mindful of how these methods interact. For example, after reading a number with `nextInt()`, the newline character entered by the user remains in the input buffer. If you then try to read a string with `nextLine()`, it will immediately consume that leftover newline character and return an empty string. A common practice to avoid this is to add an extra `scanner.nextLine();` call after reading a number to consume the rest of the line. Properly handling user input is crucial for creating robust and user-friendly console applications.