Scanner Class in Java: Types of Constructors & Methods, How to Use [With Examples]
Updated on Nov 14, 2022 | 7 min read | 10.1k views
Share:
For working professionals
For fresh graduates
More
Updated on Nov 14, 2022 | 7 min read | 10.1k views
Share:
Table of Contents
Anyone who works with the Java programming language is well aware of Scanner class in Java. And for aspiring Java Developers who don’t know what Scanner class is and how to use Scanner class in Java, this article is the perfect introduction to it.
In this post, we’ll engage in a detailed discussion of Scanner class in Java, its different methods, and how they function. So, if you are looking forward to knowing more about Scanner class in Java, keep reading till the end!
Check out our free courses to get an edge over the competition.
The Scanner class in Java is primarily used to obtain user input. The java.util package contains it. The Scanner class not only extends Object class, but it can also implement Iterator and Closeable interfaces. It fragments the user input into tokens using a delimiter, which is by default, whitespace.
It is pretty easy to use the Scanner class – first, you create an object of the class and then use any of the available methods present in the Scanner class documentation.
Besides being one of the simplest ways of obtaining user input data, the Scanner class is extensively used to parse text for strings and primitive types by using a regular expression. For instance, you can use Scanner class to get input for different primitive types like int, long, double, byte, float, and short, to name a few.
Check out upGrad’s Advanced Certification in Cloud Computing
You can declare Java Scanner class like so:
public final class Scanner
extends Object
implements Iterator<String>
If you wish to obtain the instance of the Scanner class that reads user input, you have to pass the input stream (System.in) in the constructor of Scanner class, as follows:
Scanner in = new Scanner(“Hello upGrad”);
Read: Top 6 Reasons Why Java Is So Popular With Developers
Here are the six commonly used Scanner class constructors:
Check out upGrad’s Advanced Certification in Blockchain
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
Just like Scanner class constructors, there’s also a comprehensive suite of Scanner class methods, each serving a unique purpose. You can use the Scanner class methods for different data types. Below is a list of the most widely used Scanner class methods:
Check out: Java Developer Salary in India
As we mentioned before, using the Scanner class in Java is quite easy. Below is an example demonstrating how to implement Scanner class using the nextLine() method:
import java.util.*;
public class ScannerExample {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print(“Enter your name: “);
String name = in.nextLine();
System.out.println(“Name is: ” + name);
in.close();
}
}
If you run this program, it will deliver the following output:
Enter your name: John Hanks
Name is: John Hanks
This article covers the fundamentals of the Scanner class in Java. If you acquaint yourself with the Scanner class constructs and methods, with time and continual practice, you will master the craft of how to use Scanner class in Java programs.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources