java.util.Scanner Class:
It is used to read input from keyboard. It breaks the input provided into tokens using a delimeter (Whitespace). It extends Object class and implements Iterator & Closeable interface. To use Scanner class we have to import java.util.Scanner package.
Commonly used methods of Scanner class:
public String next() : returns the next token from the scanner.
public int nextInt(): scans the next token as an int value.
public float nextFloat(): scans the next token as a float value
public double nextDouble(): scans the next token as a double value.
Example:
import java.util.Scanner;
class ScannerDemo{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
System.out.println("Enter your ID");
int id=scan.nextInt(); // reading ID provided by user
System.out.println("Enter your name");
String name=scan.next(); // reading name provided by user
System.out.println("Enter your Salary");
double salary=scan.nextDouble(); // reading salary provided by user
System.out.println("ID:"+id+" name:"+name+" salary:"+salary);
scan.close();
}
}
Output:
Enter your ID
101
Enter your name
Ajay
Enter your Salary
450000
ID:101 name:Ajay Salary:15000
0 Comment(s)