Java Scanner Class
Java Scanner Class
Java Scanner Class
Java Scanner class comes under the java.util package. Java has various ways to read input from
the keyboard, the java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a delimiter that is whitespace by
default. It provides many methods to read and parse various primitive values.
Java Scanner class is widely used to parse text for string and primitive types using a regular
expression.
Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
SN Constructor Description
1) Scanner(File source) It constructs a new Scanner that produces
values scanned from the specified file.
2) Scanner(File source, String charsetName) It constructs a new Scanner that produces
values scanned from the specified file.
3) Scanner(InputStream source) It constructs a new Scanner that produces
values scanned from the specified input
stream.
4) Scanner(InputStream source, String It constructs a new Scanner that produces
charsetName) values scanned from the specified input
stream.
5) Scanner(Readable source) It constructs a new Scanner that produces
values scanned from the specified source.
6) Scanner(String source) It constructs a new Scanner that produces
values scanned from the specified string.
7) Scanner(ReadableByteChannel source) It constructs a new Scanner that produces
values scanned from the specified channel.
8) Scanner(ReadableByteChannel source, It constructs a new Scanner that produces
String charsetName) values scanned from the specified channel.
9) Scanner(Path source) It constructs a new Scanner that produces
values scanned from the specified file.
10) Scanner(Path source, String charsetName) It constructs a new Scanner that produces
values scanned from the specified file.
Java Scanner Class Methods
The following are the list of Scanner methods:
Example 1
1. import java.util.*;
2. public class ScannerClassExample1 {
3. public static void main(String args[]){
4. String s = "Hello, This is JavaTpoint.";
5. //Create scanner Object and pass string in it
6. Scanner scan = new Scanner(s);
7. //Check if the scanner has a token
8. System.out.println("Boolean Result: " + scan.hasNext());
9. //Print the string
10. System.out.println("String: " +scan.nextLine());
11. scan.close();
12. System.out.println("--------Enter Your Details-------- ");
13. Scanner in = new Scanner(System.in);
14. System.out.print("Enter your name: ");
15. String name = in.next();
16. System.out.println("Name: " + name);
17. System.out.print("Enter your age: ");
18. int i = in.nextInt();
19. System.out.println("Age: " + i);
20. System.out.print("Enter your salary: ");
21. double d = in.nextDouble();
22. System.out.println("Salary: " + d);
23. in.close();
24. }
25. }
To create an object of Scanner class, we usually pass the predefined object System.in,
which represents the standard input stream. We may pass an object of class File if we
want to read input from a file.
To read numerical values of a certain data type XYZ, the function to use is nextXYZ().
For example, to read a value of type short, we can use nextShort()
To read strings, we use nextLine().
To read a single character, we use next().charAt(0). next() function returns the next
token/word in the input as a string and charAt(0) funtion returns the first character in that
string.
import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
String name = sc.nextLine();
// Character input
char gender = sc.next().charAt(0);
// Numerical data input
// byte, short and float can be read
// using similar-named functions.
int age = sc.nextInt();
long mobileNo = sc.nextLong();
double cgpa = sc.nextDouble();
// Print the values to check if input was correctly obtained.
System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("CGPA: "+cgpa);
}
}
import java.util.Scanner;
public class ScannerDemo2
{
public static void main(String[] args)
{
// Declare an object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// Initialize sum and count of input elements
int sum = 0, count = 0;
// Check if an int value is available
while (sc.hasNextInt())
{
// Read an int value
int num = sc.nextInt();
sum += num;
count++;
}
int mean = sum / count;
System.out.println("Mean: " + mean);
}
}
import java.util.Scanner;
I'll go step by step. First of all to understand this, you need to know what a package is?
In java, packages are the container for classes and whenever we want to use the features
of another classes that are defined in another package,we use import keyword.
Now, let's see what java.util.Scanner means !!
In java we use "." to denote the hierarchy. So the above statement will denote -
java\util\Scanner in Windows environment.