Scanner class in java
The Scanner is a built-in class in java used for read the input from the user in java programming. The Scanner class is defined inside the java.util package.
The Scanner class implements Iterator interface.
The Scanner class provides the easiest way to read input in a Java program.
🔔 The Scanner object breaks its input into tokens using a delimiter pattern, the default delimiter is whitespace.
The Scanner class in java has the following constructors.
S.No. | Constructor with Description |
---|---|
1 |
Scanner(InputStream source)
It creates a new Scanner that produces values read from the specified input stream. |
2 |
Scanner(InputStream source, String charsetName)
It creates a new Scanner that produces values read from the specified input stream. |
3 |
Scanner(File source)
It creates a new Scanner that produces values scanned from the specified file. |
4 |
Scanner(File source, String charsetName)
It creates a new Scanner that produces values scanned from the specified file. |
5 |
Scanner(String source)
It creates a new Scanner that produces values scanned from the specified string. |
6 |
Scanner(Readable source)
It creates a new Scanner that produces values scanned from the specified source. |
7 |
Scanner(ReadableByteChannel source)
It creates a new Scanner that produces values scanned from the specified channel. |
8 |
Scanner(ReadableByteChannel source, String charsetName)
It creates a new Scanner that produces values scanned from the specified channel. |
The Scanner class in java has the following methods.
S.No. | Methods with Description |
---|---|
1 |
String next()
It reads the next complete token from the invoking scanner. |
2 |
String next(Pattern pattern)
It reads the next token if it matches the specified pattern. |
3 |
String next(String pattern)
It reads the next token if it matches the pattern constructed from the specified string. |
4 |
boolean nextBoolean()
It reads a boolean value from the user. |
5 |
byte nextByte()
It reads a byte value from the user. |
6 |
double nextDouble()
It reads a double value from the user. |
7 |
float nextFloat()
It reads a floating-point value from the user. |
8 |
int nextInt()
It reads an integer value from the user. |
9 |
long nextLong()
It reads a long value from the user. |
10 |
short nextShort()
It reads a short value from the user. |
11 |
String nextLine()
It reads a string value from the user. |
12 |
boolean hasNext()
It returns true if the invoking scanner has another token in its input. |
13 |
void remove()
It is used when remove operation is not supported by this implementation of Iterator. |
14 |
void close()
It closes the invoking scanner. |
Let's consider an example program to illustrate methods of Scanner class.
import java.util.Scanner;
public class ScannerClassExample {
public static void main(String[] args) {
Scanner read = new Scanner(System.in); // Input stream is used
System.out.print("Enter any name: ");
String name = read.next();
System.out.print("Enter your age in years: ");
int age = read.nextInt();
System.out.print("Enter your salary: ");
double salary = read.nextDouble();
System.out.print("Enter any message: ");
read = new Scanner(System.in);
String msg = read.nextLine();
System.out.println("\n------------------------------------------");
System.out.println("Hello, " + name);
System.out.println("You are " + age + " years old.");
System.out.println("You are earning Rs." + salary + " per month.");
System.out.println("Words from " + name + " - " + msg);
}
}
When we execute the above code, it produce the following output.