Scanner sc = new Scanner(System.in)
It is a in-build class present in java.util
The inbuilt class is used to take the input from user through console.
Scanner class uses diff input methods:
Steps to use scanner class methods
Scanner sc = new Scanner(System.in);variable.methodName(), eg: sc.nextInt()
Example:
import java.util.Scanner;
public class MyScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int a= sc.nextInt();
System.out.println("Your Entered numer is "+a);
}
}
program to take name, height, weight, mail from user and display it
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
String name = sc.next();
System.out.println("Enter your Phone number");
Long phone = sc.nextLong();
System.out.println("Enter your height");
Float height = sc.nextFloat();
System.out.println("Enter your weight");
Float weight = sc.nextFloat();
sc.nextLine();
System.out.println("Enter your mail");
String mail = sc.next();
System.out.println("Your name is "+name);
System.out.println("Your phone number is "+phone);
System.out.println("Your height is "+height);
System.out.println("Your weight is "+weight);
System.out.println("Your mail is "+mail);
sc.close();
}
}