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:
- Scanner(File source) — It creates a new Scanner to generate values scanned from a particular file.
- Scanner(InputStream source) — It creates a new Scanner to produce values scanned from a specified input stream.
- Scanner(Readable source) — It creates a new Scanner to deliver values scanned from a specified source.
- Scanner(String source) — It creates a new Scanner to produce values scanned from a particular string.
- Scanner(ReadableByteChannel source) — It creates a new Scanner to produce values scanned from a specified channel.
- 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:
- void [close()] — This method is used to close the scanner.
- pattern [delimiter()] — This method helps to get the Pattern that is currently being used by the Scanner class to match delimiters.
- Stream<MatchResult> [findAll()] — It gives a stream of match results that match the specified pattern string.
- String [findInLine()] — It helps to find the next occurrence of a pattern created from a specified string. This method does not consider delimiters.
- String [nextLine()] — It is used to get the input string that was skipped of the Scanner object.
- IOException [ioException()] — This method helps to obtain the IOException last projected by the Scanner’s readable.
- Locale [locale()] — It fetches a Locale of the Scanner class.
- MatchResult [match()] — It offers the match result of the last scanning operation performed by the Scanner.
- BigDecimal [nextBigDecimal()] — This method is used to scan the next token of the input as a BigDecimal.
- BigInteger [nextBigInteger()] — This method scans the next token of the input as a BigInteger.
- byte [nextByte()] — It scans the next token of the user input as a byte value.
- double [nextDouble()] — It scans the next token of the user input as a double value.
- float [nextFloat()] — This method scans the next token of the input as a float value.
- int [nextInt()] — This method is used to scan the next token of the input as an Int value.
- 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.