Scanner Class

Scanner is a simple text scanner, which parse string into tokens. Scanner can parse primitive data type and string. Given string is tokenized using delimiter, which is white-space by default.


Scanner class available in java.util package. It doesn't come in java.io package.



Options Usage
int nextInt() Returns the next tokens as an int. If next token is not an integer, InputMismatchException is thrown.
long nextLong() Returns the next tokens as an long. If next token is not an integer, InputMismatchException is thrown.
float nextFloat() Returns the next tokens as an float. If next token is not an integer, InputMismatchException is thrown.
double nextDouble Returns the next tokens as an double. If next token is not an integer, InputMismatchException is thrown.
String next() return next token as a string. String token must ended with whitespace or new line. If there is no such elements then NoSuchElementException will be thrown
String nextLine() Return current line
void close() Closes the scanner.


Take input from user as int and float


 import java.util.*; 
class scan
{
public static void main(String arg[])
{
Scanner scan=new Scanner(System.in);
int a=scan.nextInt();
float b=scan.nextFloat();
System.out.println("a:"+a);
System.out.println("b:"+b);

}
}
/* Input given by user 12 2.333
Output:
a:12
b:2.333
*/