Scanner Class in Java – java.util.package

Java has different methods to read input from the keyword and the Scanner class in one of those methods. It is a part of the java.util.package and is used to break the input into tokens.

This class provides various ways to read and parse different primitive values and uses the delimiter which is whitespace by default to carry out its function.

The main function of this class is to parse text for string and primitive types by making the use of a regular expression. The object class is extended and the Iterator and Closeable interfaces are implemented by this class.

Here is a simple scanner class using if else statement in java.

Scanner Class Declaration

The following is the declaration of this class:

  • public final class Scanner extends Object   implements Iterator<String>

Java Scanner Class Constructors

The following are the constructors that are used in the Java Scanner class:

  • Scanner(File source):

This constructor is used to create a new scanner that scans a particular file and create values.

  • Scanner(File source, String charsetName):

This constructor is also used to create a new scanner that scans a particular file and create values.

  • Scanner(InputStream source):

This constructor is used to create a new scanner that scans a particular input stream and create values.

  • Scanner(InputStream source, String charsetName):

This constructor is also used to create a new scanner that scans a particular input stream and create values.

  • Scanner(Readable source):

This constructor is used to create a new scanner that scans a particular source and creates values.

  • Scanner(String source):

This constructor is used to create a new scanner that scans a particular String and creates values.

  • Scanner(ReadableByteChannel source):

This constructor is used to create a new scanner that scans a particular channel and creates values.

  • Scanner(ReadableByteChannel source, String charsetName):

This constructor is also used to create a new scanner that scans a particular channel and creates values.

  • Scanner(Path source):

This constructor is used to create a new scanner that scans a particular file and creates values.

  • Scanner(Path source, String charsetName):

This constructor is also used to create a new scanner that scans a particular file and creates values.

Scanner Class Methods

The following are the methods that are used in the Java Scanner class:

  • void close():

This method closes the scanner.

  • pattern delimiter():

This method figures out the pattern that is presently being used by the scanner class in order to match the delimiters.

  • Stream<MatchResult> findAll():

This method is used to figure out the stream of results that match the given pattern string.

  • string findInLine():

This method figures out the following occurrence of a pattern that is constructed from a particular string by ignoring the delimiters.

  • string findWithinHorizon():

This method is also used to figure out the following occurrence of a pattern that is constructed from a particular string by ignoring the delimiters.

  • boolean hasNext():

This method is used to check whether the scanner has another token in its input or not.

  • boolean hasNextBigDecimal():

This method is used to figure out whether the following token in the scanner’s input can be interpreted as a BigDecimal through the nextBigDecimal operation or not.

  • boolean hasNextBigInteger():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a BigInteger through the nextBigInteger operation otherwise returns false.

  • boolean hasNextBoolean():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a Boolean through the nextBoolean operation otherwise returns false.

  • boolean hasNextByte():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a Byte through the nextByte operation otherwise returns false.

  • boolean hasNextDouble():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a Double through the nextDouble operation otherwise returns false.

  • boolean hasNextFloat():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a Float through the nextFloat operation otherwise returns false.

  • boolean hasNextInt():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as an integer through the nextInt operation otherwise returns false.

  • boolean hasNextLine():

This method returns true if the scanner has another line in the input otherwise returns false.

  • boolean hasNextLong():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a Long through the nextLong operation otherwise returns false.

  • boolean hasNextShort():

This method returns true in the case in which the following token in the scanner’s input can be interpreted as a Short through the nextShort operation otherwise returns false.

  • IOException ioException():

This method finds out the last thrown IOException by the readable of the scanner.

  • Locale locale():

This method finds out the locale of this scanner class.

  • MatchResult match():

This method finds out the match result of the last scanning operation that the scanner performed.

  • String next():

This method finds out the following complete token from the scanner that is currently being used.

  • BigDecimal nextBigDecimal():

This method is used to scan the following token of the input as a BigDecimal and to return it.

  • BigInteger nextBigInteger():

This method is used to scan the following token of the input as a BigInteger and to return it.

  • boolean nextBoolean():

This method is used to scan the following token of the input as a Boolean value.

  • byte nextByte():

This method is used to scan the following token of the input as a byte and to return it.

  • double nextDouble():

This method is used to scan the following token of the input as a double and to return it.

  • float nextFloat():

This method is used to scan the following token of the input as a float and to return it.

  • int nextInt():

This method is used to scan the following token of the input as an integer and to return it.

  • string nextLine():

This method finds out the input string that the scanner object had skipped and returns it.

  • long nextLong():

This method is used to scan the following token of the input as a long and to return it.

  • short nextShort():

This method is used to scan the following token of the input as a short and to return it.

  • int radix():

This method finds out the default radix of the currently used scanner.

  • void remove():

This method is used in the situation in which the implementation of Iterator does not support the remove method.

  • scanner reset():

This method resets the currently used scanner.

  • scanner skip():

This method is used to skip input that matches a particular pattern by ignoring the delimiters.

  • Stream<String> tokens():

This method finds out a stream of delimiter-separated tokens from the currently used scanner object and returns it.

  • string toString():

This method finds out the string representation of the currently used scanner and returns it.

  • scanner useDelimiter():

This method sets the delimiting pattern of the scanner that is in use to a particular pattern.

  • scanner useLocal():

This method sets the locale object of the scanner to a particular locale.

  • scanner useRadix():

This method sets the currently used scanners to default to a particular radix.

Real-Time Example: 

In this example, I am going to show you how to make a basic calculator using Scanner class. 

Leave a Reply

Your email address will not be published. Required fields are marked *