Java Utility Library

Java.util.Scanner Class



Java Scanner Class

Java.util package provides a Scanner class. A simple text scanner parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

A scanning operation may block waiting for input and not safe for multi-threaded use without external synchronization.

Class declaration

The declaration of java.util.Scanner class is:

public final class Scanner
  extends Object
    implements Iterator<String>, Closeable

Class Constructors

S.NConstructors & Description
1. Scanner(File source)
Constructs a new Scanner that produces values scanned from the specified file.
2. Scanner(File source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified file.
3. Scanner(InputStream source)
Constructs a new Scanner that produces values scanned from the specified input stream.
4. Scanner(InputStream source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified input stream.
5. Scanner(Path source)
Constructs a new Scanner that produces values scanned from the specified file.
6. Scanner(Path source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified file.
7. Scanner(Readable source)
Constructs a new Scanner that produces values scanned from the specified source.
8. Scanner(ReadableByteChannel source)
Constructs a new Scanner that produces values scanned from the specified channel.
9. Scanner(ReadableByteChannel source, String charsetName)
Constructs a new Scanner that produces values scanned from the specified channel.
10. Scanner(String source)
Constructs a new Scanner that produces values scanned from the specified string.

java.util.Scanner Methods

The java.util.Scanner class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void close()
closes the scanner.
2. Pattern delimiter()
Returns the pattern the Scanner is currently using to match delimiters.
3. String findInLine(Pattern pattern)
Attempts to find the next occurrence of the specified pattern ignoring delimiters.
4. String findInLine(String pattern)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
5. String findWithinHorizon(Pattern pattern, int horizon)
Attempts to find the next occurrence of the specified pattern.
6. String findWithinHorizon(String pattern, int horizon)
Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
7. boolean hasNext()
Returns true if the scanner has another token in its input.
8. boolean hasNext(Pattern pattern)
Returns true if the next complete token matches the specified pattern.
9. boolean hasNext(String pattern)
Returns true if the next token matches the pattern constructed from the specified string.
10. boolean hasNextBigDecimal()
Returns true if the next token in the scanner's input can be interpreted as a BigDecimal value using the nextBigDecimal() method.
11. boolean hasNextBigInteger()
Returns true if the next token in the scanner's input can be interpreted as a BigInteger value in the default radix using the nextBigInteger() method.
12. boolean hasNextBigInteger(int radix)
Returns true if the next token in the scanner's input can be interpreted as a BigInteger value in the specified radix using the nextBigInteger() method.
13. boolean hasNextBoolean()
Returns true if the next token in the scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".
14. boolean hasNextByte()
Returns true if the next token in the scanner's input can be interpreted as a byte value in the default radix using the nextByte() method.
15. boolean hasNextByte(int radix)
Returns true if the next token in the scanner's input can be interpreted as a byte value in the specified radix using the nextByte() method.
16. boolean hasNextDouble()
Returns true if the next token in the scanner's input can be interpreted as a double value using the nextDouble() method.
17. boolean hasNextFloat()
Returns true if the next token in the scanner's input can be interpreted as a float value using the nextFloat() method.
18. boolean hasNextInt()
Returns true if the next token in the scanner's input can be interpreted as an int value in the default radix using the nextInt() method.
19. boolean hasNextInt(int radix)
Returns true if the next token in the scanner's input can be interpreted as an int value in the specified radix using the nextInt() method.
20. boolean hasNextLine()
Returns true if there is another line in the input of the scanner.
21. boolean hasNextLong()
Returns true if the next token in the scanner's input can be interpreted as a long value in the default radix using the nextLong() method.
22. boolean hasNextLong(int radix)
Returns true if the next token in the scanner's input can be interpreted as a long value in the specified radix using the nextLong() method.
23. boolean hasNextShort()
Returns true if the next token in the scanner's input can be interpreted as a short value in the default radix using the nextShort() method.
24. boolean hasNextShort(int radix)
Returns true if the next token in the scanner's input can be interpreted as a short value in the specified radix using the nextShort() method.
25. IOException ioException()
Returns the IOException last thrown by the Scanner's underlying Readable.
26. Locale locale()
Returns the scanner's locale.
27. MatchResult match()
Returns the match result of the last scanning operation performed by the scanner.
28. String next()
Finds and returns the next complete token from the scanner.
29. String next(Pattern pattern)
Returns the next token if it matches the specified pattern.
30. String next(String pattern)
Returns the next token if it matches the pattern constructed from the specified string.
31. BigDecimal nextBigDecimal()
Scans the next token of the input as a BigDecimal.
32. BigInteger nextBigInteger()
Scans the next token of the input as a BigInteger.
33. BigInteger nextBigInteger(int radix)
Scans the next token of the input as a BigInteger.
34. boolean nextBoolean()
Scans the next token of the input as a boolean.
35. byte nextByte()
Scans the next token of the input as a byte.
36. byte nextByte(int radix)
Scans the next token of the input as a byte.
37. double nextDouble()
Scans the next token of the input as a double.
38. float nextFloat()
Scans the next token of the input as a float.
39. int nextInt()
Scans the next token of the input as an int.
40. int nextInt(int radix)
Scans the next token of the input as an int.
41. String nextLine()
Advances the scanner past the current line and returns the input that was skipped.
42. long nextLong()
Scans the next token of the input as a long.
43. long nextLong(int radix)
Scans the next token of the input as a long.
44. short nextShort()
Scans the next token of the input as a short.
45. short nextShort(int radix)
Scans the next token of the input as a short.
46. int radix()
Returns the scanner's default radix.
47. void remove()
The remove operation is not supported by the implementation of Iterator.
48. Scanner reset()
Resets the scanner.
49. Scanner skip(Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
50. Scanner skip(String pattern)
Skips input that matches a pattern constructed from the specified string.
51. String toString()
Returns the string representation of the Scanner.
52. Scanner useDelimiter(Pattern pattern)
Sets the scanner's delimiting pattern to the specified pattern.
53. Scanner useDelimiter(String pattern)
Sets the scanner's delimiting pattern to a pattern constructed from the specified String.
54. Scanner useLocale(Locale locale)
Sets the scanner's locale to the specified locale.
55. Scanner useRadix(int radix)
Sets the scanner's default radix to the specified radix.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object