Scanner Class
Java offers several ways to interpret keyboard input. The most well-known method of reading data is the scanner. This class belongs to java.util package. We can get different input types, such as int, float, double, byte, short, etc.
Syntax:
Scanner b=new Scanner (System.in);
Some common methods are
1. nextInt():-obtaining an integer number from the user
Syntax:- public int nextInt();
2. nextLine():- read a String value from the user
Syntax:- public String nextLine();
3. nextBoolean():- reads a Boolean value from the user
Syntax:- public boolean nextBoolean();
4. nextFloat():- reads float value from the user
Syntax:- public float nextFloat();
5. nextShort():- reads a short value from the user
Syntax:- public short nextShort();
6. reset():- reset the scanner which is used
Syntax:- public Scanner reset();
7. close() :- close the scanner
Syntax:- public void close();
8. next():-the following token from the scanner
Syntax:-public String next();
Code Example
import java.util.*;
class EMP {
public static void main(String args[])
{
System.out.println("Enter Your Details :- ");
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.nextLine();
System.out.println("your Name is: " + name);
System.out.print("Enter your age: ");
int i = in.nextInt();
System.out.println("your Age: " + i);
System.out.print("Enter your salary: ");
double d = in.nextDouble();
System.out.println("your Salary: " + d);
in.close();
}
}
RANREV INFOTECH