Scanner Class in Java: Types of Constructors & Methods, How to Use [With Examples]
By Rohan Vats
Updated on Nov 14, 2022 | 7 min read | 10.75K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Nov 14, 2022 | 7 min read | 10.75K+ 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:
Software Development Courses to upskill
Explore Software Development Courses for Career Progression
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.
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
A pointer is a variable in languages like C and C++ that stores another variable's address value. They are not considered to be beginner-friendly for new programmers. Java keeps the simplicity of code in mind, and usage of concepts like pointers can increase complexity. Their utilization can also lead to potential errors. Also, there are security issues when we use pointers as the programmer can directly access the memory, and by mistake, there can be many repercussions. A distinct abstraction layer is added by not including the concept of pointers in Java. Also, the usage of pointers can make garbage collection error-prone and slow.
Both Java and C++ are object-oriented programming languages. Java was created by James Gosling, whereas Bjarne Stroustrup developed C++. Java is a platform-independent language and can be used on any OS using the byte code produced by JVM (Java Virtual Machine), whereas C++ is dependent on the platform. Java is portable to any operating system, whereas C++ is not portable. Java is a compiled and interpreted language, whereas C++ is just a compiled language. Java doesn't support multiple inheritance directly; you have to use interfaces to achieve it. On the other hand, you can directly achieve multiple inheritance in C++.
An abstract class in Java cannot be instantiated or have any class objects, but it can be subclassed. An interface in Java specifies behavior that all the child classes must implement. In interfaces, only abstract methods can be used, whereas, in abstract classes, both abstract and non-abstract methods can co-exist. Only static and final variables can be used in interfaces, whereas abstract classes can also use non-static and non-final variables. Multiple inheritance is done by using interfaces which is not the case with abstract classes. An abstract class can easily implement an interface, whereas an interface cannot implement an abstract class.
408 articles published
Rohan Vats is a Senior Engineering Manager with over a decade of experience in building scalable frontend architectures and leading high-performing engineering teams. Holding a B.Tech in Computer Scie...
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