191 Java Scanner Class

Introduction

The Java Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.Following are the important points about Scanner −

  • A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

  • A scanning operation may block waiting for input.

  • A Scanner is not safe for multithreaded use without external synchronization.

Class declaration

Following is the declaration for java.util.Scanner class −

public final class Scanner extends Object implements Iterator<String>


Class constructors

Sr.No.Constructor & Description
1

Scanner(File source)

This constructs a new Scanner that produces values scanned from the specified file.

2

Scanner(File source, String charsetName)

This constructs a new Scanner that produces values scanned from the specified file.

3

Scanner(File source, Charset charset)

This constructs a new Scanner that produces values scanned from the specified file.

4

Scanner(InputStream source)

This constructs a new Scanner that produces values scanned from the specified input stream.

5

Scanner(InputStream source, String charsetName)

This constructs a new Scanner that produces values scanned from the specified input stream.

6

Scanner(InputStream source, Charset charset)

This constructs a new Scanner that produces values scanned from the specified input stream.

7

Scanner(Readable source)

This constructs a new Scanner that produces values scanned from the specified source.

8

Scanner(String source)

This constructs a new Scanner that produces values scanned from the specified source.

9

Scanner(ReadableByteChannel source)

This constructs a new Scanner that produces values scanned from the specified channel.

10

Scanner(ReadableByteChannel source, String charsetName)

This constructs a new Scanner that produces values scanned from the specified channel.

11

Scanner(ReadableByteChannel source, Charset charset)

This constructs a new Scanner that produces values scanned from the specified channel.

12

Scanner(Path source)

This constructs a new Scanner that produces values scanned from the specified file.

13

Scanner(Path source, String charsetName)

This constructs a new Scanner that produces values scanned from the specified file.

14

Scanner(Path source, Charset charset)

This constructs a new Scanner that produces values scanned from the specified file.

Class methods

Sr.No.Method & Description
1void close()

This method closes this scanner.

2Pattern delimiter()

This method returns the Pattern this Scanner is currently using to match delimiters.

3Stream<MatchResult> findAll​(String patString)

This method returns a stream of match results that match the provided pattern string.

4String findInLine(Pattern pattern)

This method attempts to find the next occurrence of the specified pattern ignoring delimiters.

5String findWithinHorizon(Pattern pattern, int horizon)

This method attempts to find the next occurrence of the specified pattern.

6boolean hasNext()

This method returns true if this scanner has another token in its input.

7boolean hasNextBigDecimal()

This method returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method.

8boolean hasNextBigInteger()

This method returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method.

9boolean hasNextBoolean()

This method returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".

10boolean hasNextByte()

This method returns true if the next token in this scanner's input can be interpreted as a byte value in the default radix using the nextByte() method.

11boolean hasNextDouble()

This method returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method.

12boolean hasNextFloat()

This method Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method.

13boolean hasNextInt()

This method returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method.

14boolean hasNextLine()

This method returns true if there is another line in the input of this scanner.

15boolean hasNextLong()

This method returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method.

16boolean hasNextShort()

This method returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method.

17IOException ioException()

This method returns the IOException last thrown by this Scanner's underlying Readable.

18Locale locale()

This method returns this scanner's locale.

19MatchResult match()

This method returns the match result of the last scanning operation performed by this scanner.

20String next()

This method finds and returns the next complete token from this scanner.

21BigDecimal nextBigDecimal()

This method scans the next token of the input as a BigDecimal.

22BigInteger nextBigInteger()

This method Scans the next token of the input as a BigInteger.

23boolean nextBoolean()

This method scans the next token of the input into a boolean value and returns that value.

24byte nextByte()

This method scans the next token of the input as a byte.

25double nextDouble()

This method scans the next token of the input as a double.

26float nextFloat()

This method scans the next token of the input as a float.

27int nextInt()

This method scans the next token of the input as an int.

28String nextLine()

This method advances this scanner past the current line and returns the input that was skipped.

29long nextLong()

This method scans the next token of the input as a long.

30short nextShort()

This method scans the next token of the input as a short.

31int radix()

This method returns this scanner's default radix.

32void remove()

The remove operation is not supported by this implementation of Iterator.

33Scanner reset()

This method resets this scanner.

34Scanner skip(Pattern pattern)

This method skips input that matches the specified pattern, ignoring delimiters.

35Stream<String> tokens()

Returns a stream of delimiter-separated tokens from this scanner.

36String toString()

This method returns the string representation of this Scanner.

37Scanner useDelimiter(Pattern pattern)

This method sets this scanner's delimiting pattern to the specified pattern.

38Scanner useLocale(Locale locale)

This method sets this scanner's locale to the specified locale.

39Scanner useRadix(int radix)

This method Sets this scanner's default radix to the specified radix.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Reading a Line from Console using Scanner Class Example

The following example shows the usage of Java Scanner nextLine() to read a line from Console and close() method to close the scanner. We've created a scanner object using a given string. Then we printed the string using nextLine() method and then scanner is closed using close() method.

package com.codeheap; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6"; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // print the next line of the string System.out.println(scanner.nextLine());
// close the scanner System.out.println("Closing Scanner..."); scanner.close(); System.out.println("Scanner Closed."); } }

Output

Let us compile and run the above program, this will produce the following result −

Hello World! 3 + 3.0 = 6
Closing Scanner...
Scanner Closed.

Comments

Popular posts from this blog

79 HTML - Tables