
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java Vs. C++
- JVM - Java Virtual Machine
- Java - JDK vs JRE vs JVM
- Java - Environment Setup
- Java - Hello World Program
- Java - Comments
- Java - Basic Syntax
- Java - Variables
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - User Input
- Java - Date & Time
Java Operators
- Java - Operators
- Java - Arithmetic Operators
- Java - Assignment Operators
- Java - Relational Operators
- Java - Logical Operators
- Java - Bitwise Operators
- Java Operator Precedence & Associativity
Java Control Statements
- Java - Decision Making
- Java - If Else Statement
- Java - Switch Statement
- Java - Loop Control
- Java - For Loop
- Java - For-Each Loop
- Java - While Loop
- Java - Do While Loop
- Java - Break Statement
- Java - Continue Statement
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner
- Java - Arrays
- Java - Strings
- Java - Date
- Java - ArrayList
- Java - Vector
- Java - Stack
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - IdentityHashMap
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection
- Java - Array
Java Useful Resources
Java - Util 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 |
---|---|
1 |
void close()
This method closes this scanner. |
2 |
Pattern delimiter()
This method returns the Pattern this Scanner is currently using to match delimiters. |
3 |
Stream<MatchResult> findAll(String patString)
This method returns a stream of match results that match the provided pattern string. |
4 |
String findInLine(Pattern pattern)
This method attempts to find the next occurrence of the specified pattern ignoring delimiters. |
5 |
String findWithinHorizon(Pattern pattern, int horizon)
This method attempts to find the next occurrence of the specified pattern. |
6 |
boolean hasNext()
This method returns true if this scanner has another token in its input. |
7 |
boolean hasNextBigDecimal()
This method returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method. |
8 |
boolean 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. |
9 |
boolean 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". |
10 |
boolean 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. |
11 |
boolean 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. |
12 |
boolean 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. |
13 |
boolean 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. |
14 |
boolean hasNextLine()
This method returns true if there is another line in the input of this scanner. |
15 |
boolean 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. |
16 |
boolean 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. |
17 |
IOException ioException()
This method returns the IOException last thrown by this Scanner's underlying Readable. |
18 |
Locale locale()
This method returns this scanner's locale. |
19 |
MatchResult match()
This method returns the match result of the last scanning operation performed by this scanner. |
20 |
String next()
This method finds and returns the next complete token from this scanner. |
21 |
BigDecimal nextBigDecimal()
This method scans the next token of the input as a BigDecimal. |
22 |
BigInteger nextBigInteger()
This method Scans the next token of the input as a BigInteger. |
23 |
boolean nextBoolean()
This method scans the next token of the input into a boolean value and returns that value. |
24 |
byte nextByte()
This method scans the next token of the input as a byte. |
25 |
double nextDouble()
This method scans the next token of the input as a double. |
26 |
float nextFloat()
This method scans the next token of the input as a float. |
27 |
int nextInt()
This method scans the next token of the input as an int. |
28 |
String nextLine()
This method advances this scanner past the current line and returns the input that was skipped. |
29 |
long nextLong()
This method scans the next token of the input as a long. |
30 |
short nextShort()
This method scans the next token of the input as a short. |
31 |
int radix()
This method returns this scanner's default radix. |
32 |
void remove()
The remove operation is not supported by this implementation of Iterator. |
33 |
Scanner reset()
This method resets this scanner. |
34 |
Scanner skip(Pattern pattern)
This method skips input that matches the specified pattern, ignoring delimiters. |
35 |
Stream<String> tokens()
Returns a stream of delimiter-separated tokens from this scanner. |
36 |
String toString()
This method returns the string representation of this Scanner. |
37 |
Scanner useDelimiter(Pattern pattern)
This method sets this scanner's delimiting pattern to the specified pattern. |
38 |
Scanner useLocale(Locale locale)
This method sets this scanner's locale to the specified locale. |
39 |
Scanner 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