Taking Input in Java:

In Java, we take input from the user using various methods. The most common way to take input from the user is by using the Scanner class, which is the part of the java.util package.

Steps:

  • 1. Import the scanner class.
  • 2. Create a scanner object.
  • 3. Prompt the user to take input.
  • 4. Read input.
  • 5. Close the scanner object.

input.java
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);

    System.out.print("Enter your name: ");
    String name = scanner.nextLine();
  
    scanner.close();
  
    System.out.println("Hi " + name );
  }
}