Scanner Class in Java:
==> Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double,and strings.
==> Using the Scanner class in Java 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.
Example program:
package javaprogram;
import java.util.Scanner; // Import Scanner class
public class ScannerClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking String input
System.out.print("Enter your name: ");
String name = scanner.nextLine();
// Taking integer input
System.out.print("Enter your age: ");
int age = scanner.nextInt();
// Displaying input values
System.out.println("Hello, " + name + "! You are " + age + " years old.");
}
}
Top comments (0)