What is Scanner class?
Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
A scanning operation may block waiting for input.
A Scanner is not safe for multithreaded use without external synchronization.
java.util.Scanner class declaration
public final class Scanner extends Object implements Iterator
ScannerTesting.java
import java.util.Scanner;
public class ScannerTesting {
public static void main(String[] args) {
Scanner scanner = new Scanner("hello 1 2.00 false");
scanner.useDelimiter(" ");
String str = scanner.next();
int anInt = scanner.nextInt();
float aFloat = scanner.nextFloat();
boolean booleanValue = scanner.nextBoolean();
System.out.println(str + ":" + anInt + ":" + aFloat + ":" + booleanValue);
}
}
Output : hello:1:2.0:false
Recent Comments