Everything You Need To Know About Scanner Class in Java - by Upgrad - Medium

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Sign in to Medium with Google

dani pedro
daniel.pirvu2000@gmail.com

Continue as dani

To create your account, Google will share your


name, email address, and profile picture with
Medium. See Medium's privacy policy and terms of
service.
upGrad
Sep 27, 2020 · 5 min read · Listen

Everything You Need to Know About Scanner


Class in Java

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!

What is the Scanner class in Java?


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.

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

What are the different Scanner class constructors?


Here are the six commonly used Scanner class constructors:

1. Scanner(File source) — It creates a new Scanner to generate values


scanned from a particular file.

2. Scanner(InputStream source) — It creates a new Scanner to produce


values scanned from a specified input stream.

3. Scanner(Readable source) — It creates a new Scanner to deliver values


scanned from a specified source.

4. Scanner(String source) — It creates a new Scanner to produce values


scanned from a particular string.

5. Scanner(ReadableByteChannel source) — It creates a new Scanner to


produce values scanned from a specified channel.

6. Scanner(Path source) — It creates a new Scanner to generate values


scanned from a specified file.

What are the different Scanner class methods?


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:

1. void [close()] — This method is used to close the scanner.

2. pattern [delimiter()] — This method helps to get the Pattern that is


currently being used by the Scanner class to match delimiters.

3. Stream<MatchResult> [findAll()] — It gives a stream of match results that


match the specified pattern string.

4. String [findInLine()] — It helps to find the next occurrence of a pattern


created from a specified string. This method does not consider
delimiters.

5. String [nextLine()] — It is used to get the input string that was skipped of
the Scanner object.
6. IOException [ioException()] — This method helps to obtain the
IOException last projected by the Scanner’s readable.

7. Locale [locale()] — It fetches a Locale of the Scanner class.

8. MatchResult [match()] — It offers the match result of the last scanning


operation performed by the Scanner.

9. BigDecimal [nextBigDecimal()] — This method is used to scan the next


token of the input as a BigDecimal.

10. BigInteger [nextBigInteger()] — This method scans the next token of the
input as a BigInteger.

11. byte [nextByte()] — It scans the next token of the user input as a byte
value.

12. double [nextDouble()] — It scans the next token of the user input as a
double value.

13. float [nextFloat()] — This method scans the next token of the input as a
float value.

Search Medium 14. int [nextInt()] — This method is used to scan the next token of the input as Write Sign up Sign In
an Int value.

15. boolean:

[hasNext()] — This method returns true if the Scanner has another token
in the user input.
upGrad
[hasNextBigDecimal()] — This method checks if the next token in the 340 Followers
Scanner’s input can be interpreted as a BigDecimal by using the Learn skills that define the future of work. Follow
nextBigDecimal() method. upgrad.com/blog to stay updated on Data
Science, ML, Big Data, Blockchain, Digital
[hasNextBoolean()] — It checks if the next token in the Scanner’s input Marketing & MBA

can be interpreted as a Boolean using the nextBoolean() method.


Follow
[hasNextByte()] — It checks whether or not the next token in the
Scanner’s input is interpretable as a Byte using the nextBigDecimal()
method. More from Medium

[hasNextFloat()] — It checks whether or not the next token in the The PyCoach in Artificial Corner

Scanner’s input is interpretable as a Float using the nextFloat() method. 3 ChatGPT Extensions to
Automate Your Life
[hasNextInt()] — It checks whether or not the next token in the Scanner’s
input is interpretable as an Int using the nextInt() method. Josep Ferrer in Geek Culture

5 ChatGPT features to boost


your daily work
Check out: Java Developer Salary in India
Mark Schaefer

20 Entertaining Uses of
ChatGPT You Never Knew
How to use Scanner class in Java? Were Possible
As we mentioned before, using the Scanner class in Java is quite easy. Below
andrew costa in Human Parts
is an example demonstrating how to implement Scanner class using the
Today I Learned Something
nextLine() method: About My Boyfriend That No
Girl Should Ever Have to…

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();

} Help Status Writers Blog Careers Privacy Terms About


Text to speech

If you run this program, it will deliver the following output:

Enter your name: John Hanks

Name is: John Hanks

Also Read: What is Type Casting in Java | Understanding Type Casting As a


Beginner

Wrapping up
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.

This article originally published on upGrad blog.

Java Scanner Class Software Developement Boolean Programming Languages

You might also like