Java.util.Scanner Class

Java.util.Scanner Class

Introduction

The java.util.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

S.N.Constructor & Description
1Scanner(File source)

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

2Scanner(File source, String charsetName)

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

3Scanner(InputStream source)

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

4Scanner(InputStream source, String charsetName)

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

5Scanner(Readable source)

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

6Scanner(ReadableByteChannel source)

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

7Scanner(ReadableByteChannel source, String charsetName)

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

8Scanner(String source)

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

Class methods

S.N.Method & Description
1void close()

This method closes this scanner.

2Pattern delimiter()

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

3String findInLine(Pattern pattern)

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

4String findInLine(String pattern)

This method attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.

5String findWithinHorizon(Pattern pattern, int horizon)

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

6String findWithinHorizon(String pattern, int horizon)

This method attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.

7boolean hasNext()

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

8boolean hasNext(Pattern pattern)

This method returns true if the next complete token matches the specified pattern.

9boolean hasNext(String pattern)

This method returns true if the next token matches the pattern constructed from the specified string.

10boolean hasNextBigDecimal()

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

11boolean 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.

12boolean hasNextBigInteger(int radix)

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

13boolean 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”.

14boolean 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.

15boolean hasNextByte(int radix)

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

16boolean 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.

17boolean 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.

18boolean 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.

19boolean hasNextInt(int radix)

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

20boolean hasNextLine()

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

21boolean 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.

22boolean hasNextLong(int radix)

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

23boolean 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.

24boolean hasNextShort(int radix)

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

25IOException ioException()

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

26Locale locale()

This method returns this scanner’s locale.

27MatchResult match()

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

28String next()

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

29String next(Pattern pattern)

This method returns the next token if it matches the specified pattern.

30String next(String pattern)

This method returns the next token if it matches the pattern constructed from the specified string.

31BigDecimal nextBigDecimal()

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

32BigInteger nextBigInteger()

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

33BigInteger nextBigInteger(int radix)

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

34boolean nextBoolean()

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

35byte nextByte()

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

36byte nextByte(int radix)
This method scans the next token of the input as a byte.
37double nextDouble()

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

38float nextFloat()

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

39int nextInt()

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

40int nextInt(int radix)

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

41String nextLine()

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

42long nextLong()

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

43long nextLong(int radix)

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

44short nextShort()

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

45short nextShort(int radix)

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

46int radix()

This method returns this scanner’s default radix.

47void remove()

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

48Scanner reset()

This method resets this scanner.

49Scanner skip(Pattern pattern)

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

50Scanner skip(String pattern)

This method skips input that matches a pattern constructed from the specified string.

51String toString()

This method returns the string representation of this Scanner.

52Scanner useDelimiter(Pattern pattern)

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

53Scanner useDelimiter(String pattern)

This method sets this scanner’s delimiting pattern to a pattern constructed from the specified String.

54Scanner useLocale(Locale locale)

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

55Scanner 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

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.