Everything You Need to Know About Scanner Class in Java

upGrad
5 min readSep 27, 2020

--

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.
  14. int [nextInt()] — This method is used to scan the next token of the input as an Int value.
  15. boolean:
  • [hasNext()] — This method returns true if the Scanner has another token in the user input.
  • [hasNextBigDecimal()] — This method checks if the next token in the Scanner’s input can be interpreted as a BigDecimal by using the nextBigDecimal() method.
  • [hasNextBoolean()] — It checks if the next token in the Scanner’s input can be interpreted as a Boolean using the nextBoolean() method.
  • [hasNextByte()] — It checks whether or not the next token in the Scanner’s input is interpretable as a Byte using the nextBigDecimal() method.
  • [hasNextFloat()] — It checks whether or not the next token in the Scanner’s input is interpretable as a Float using the nextFloat() method.
  • [hasNextInt()] — It checks whether or not the next token in the Scanner’s input is interpretable as an Int using the nextInt() method.

Check out: Java Developer Salary in India

How to use Scanner class in Java?

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

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.

--

--

upGrad

Learn skills that define the future of work. Follow upgrad.com/blog to stay updated on Data Science, ML, Big Data, Blockchain, Digital Marketing & MBA