User input:
User input is the input given by the user. If user types the age or something computer take it as input and use this for processing and then basis on this it gives output
Using the Scanner class:
In Java, you have to use Scanner class which is the part of the java.util package to get the input from the user.
First you need to import the Scanner class at the beginner of the java file.
import java.util.Scanner;
After import the Scanner Class ,You create a Scanner object in your code this will be used to get the input method from that class. You create a Scanner object like this.
// code
Scanner scanner = new Scanner(System.in); // Create a Scanner object
Here, System.in represents the standard input stream, which usually means to take input form the keyboard.
Reading user input: Once you have a Scanner object, you can use its methods to read different types of input. The Scanner class provides methods like
nextLine() to read a line of text,
nextInt() to read an integer,
nextDouble() to read a double
and there are other explore them.
For example, to read a line of text from the user, you would do:
// code
String userName = scanner.nextLine();
This line of code reads a line of text entered by the user and stores it in the variable userName.
Using the input:
Once you've obtainer the user's input , you can use it anywhere you want . You can perform any type of operation.
Closing the Scanner: After you've finished using the Scanner object, it's good practice to close it to free up system resources. You can do this by calling the close() method on the Scanner object:
//code
scanner.close();
//Here is complete code
import java.util.Scanner; // Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = scanner.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
scanner.close();
}