- 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 :
- Scanner class part of java.util package .
- 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
Post a Comment