DEV Community

AmalaReegan
AmalaReegan

Posted on

Day24-Scanner Class in Java

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.");

    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more