Take input from user in java(Scanner class)

  •  we can take input from user by the help of Scanner class.
  • Scanner class resides in java.util package(java package is a group of similar types of classes).
  • Scanner class : 
  1.  Scanner class part of java.util package .
  2. Provides methods to read and parse input from various sources such as the keyboard or files.
  • Syntax : Scanner object name = new Scanner(System.in)
To use the Scanner class, you need to import it by the use of import keyword (java.util package)
syntax :
  • import java.util.Scanner;
After importing the Scanner class you can create an instance of it to start reading input.

Example : 
import java.util.Scanner;

public class Example {

    public static void main(String[] args) {

        Scanner manvik = new Scanner(System.in);

       System.out.println("Enter an integer: ");

        int number = manvik.nextInt();

        System.out.println("You entered: " + number);

        scanner.close();
    }

}


Comments