Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.
The Scanner input in Java should be of primitive
data types, such as int, float, double, etc., and string types.
The package of the Scanner class –
java.util.Scanner, needs to be imported to use the Scanner class.
import
java.util.Scanner;
To use the Scanner class, create an object
of the class and use any of the available methods found in the Scanner class.
Object to read from file
Scanner obj1 = new Scanner(File filename);
Object to read from the input stream
Scanner obj2 = new Scanner(inputStream
inputStreamName);
Object to read input as String
Scanner obj3 = new Scanner(String s);
Java
Scanner Class Methods
SN |
Modifier & Type |
Method |
Description |
1) |
void |
close() |
It is used to close this scanner. |
2) |
pattern |
delimiter() |
It is used to get the Pattern which the Scanner
class is currently using to match delimiters. |
3) |
Stream<MatchResult> |
findAll() |
It is used to find a stream of match results that
match the provided pattern string. |
4) |
String |
It is used to find the next occurrence of a
pattern constructed from the specified string, ignoring delimiters. |
|
5) |
string |
It is used to find the next occurrence of a
pattern constructed from the specified string, ignoring delimiters. |
|
6) |
boolean |
It returns true if this scanner has another token
in its input. |
|
7) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a BigDecimal using the nextBigDecimal()
method or not. |
|
8) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a BigDecimal using the nextBigDecimal()
method or not. |
|
9) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a Boolean using the nextBoolean()
method or not. |
|
10) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a Byte using the nextBigDecimal()
method or not. |
|
11) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a BigDecimal using the nextByte()
method or not. |
|
12) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a Float using the nextFloat() method or
not. |
|
13) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as an int using the nextInt() method or
not. |
|
14) |
boolean |
It is used to check if there is another line in
the input of this scanner or not. |
|
15) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a Long using the nextLong() method or
not. |
|
16) |
boolean |
It is used to check if the next token in this
scanner's input can be interpreted as a Short using the nextShort() method or
not. |
|
17) |
IOException |
It is used to get the IOException last thrown by
this Scanner's readable. |
|
18) |
Locale |
It is used to get a Locale of the Scanner class. |
|
19) |
MatchResult |
It is used to get the match result of the last
scanning operation performed by this scanner. |
|
20) |
String |
It is used to get the next complete token from the
scanner which is in use. |
|
21) |
BigDecimal |
It scans the next token of the input as a
BigDecimal. |
|
22) |
BigInteger |
It scans the next token of the input as a
BigInteger. |
|
23) |
boolean |
It scans the next token of the input into a
boolean value and returns that value. |
|
24) |
byte |
It scans the next token of the input as a byte. |
|
25) |
double |
It scans the next token of the input as a double. |
|
26) |
float |
It scans the next token of the input as a float. |
|
27) |
int |
It scans the next token of the input as an Int. |
|
28) |
String |
It is used to get the input string that was
skipped of the Scanner object. |
|
29) |
long |
It scans the next token of the input as a long. |
|
30) |
short |
It scans the next token of the input as a short. |
|
31) |
int |
It is used to get the default radix of the Scanner
use. |
|
32) |
void |
It is used when remove operation is not supported
by this implementation of Iterator. |
|
33) |
Scanner |
It is used to reset the Scanner which is in use. |
|
34) |
Scanner |
It skips input that matches the specified pattern,
ignoring delimiters |
|
35) |
Stream<String> |
It is used to get a stream of delimiter-separated
tokens from the Scanner object which is in use. |
|
36) |
String |
It is used to get the string representation of
Scanner using. |
|
37) |
Scanner |
It is used to set the delimiting pattern of the
Scanner which is in use to the specified pattern. |
|
38) |
Scanner |
It is used to sets this scanner's locale object to
the specified locale. |
Ex 1:
import java.util.*;
public class ScannerExample {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.nextLine();
System.out.println("Name is: " + name);
in.close();
}
}
Ex 2:
import java.util.*;
public class ScannerClassExample1 {
public
static void main(String args[]){
String s = "Hello";
Scanner scan = new Scanner(s);
//Check if the scanner has a token
System.out.println("Boolean Result: " + scan.hasNext());
//Print the string
System.out.println("String: " +scan.nextLine());
scan.close();
System.out.println("--------Enter
Your Details-------- ");
Scanner in = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = in.next();
System.out.println("Name: " + name);
System.out.print("Enter your age: ");
int
i = in.nextInt();
System.out.println("Age: " + i);
System.out.print("Enter your salary: ");
double d = in.nextDouble();
System.out.println("Salary: " + d);
in.close();
}
}
No comments:
Post a Comment