Java Scanner Class Methods
Java Scanner Class Methods
1. Methods to Check Input Availability (hasNext Methods)
• hasNext() → Checks if more input is available. Returns true if there is another token.
• hasNextInt() → Checks if the next token is an integer. Returns true if it can be parsed.
• hasNextLong() → Checks if the next token is a long number. Returns true if valid.
• hasNextDouble() → Checks if the next token is a decimal. Returns true if it can be read.
• hasNextFloat() → Checks if the next token is a float. Returns true if it is a valid float.
• hasNextShort() → Checks if the next token is a short number. Returns true if valid.
• hasNextByte() → Checks if the next token is a byte. Returns true if valid.
• hasNextBoolean() → Checks if the next token is true/false. Returns true if it is.
• hasNextLine() → Checks if another line of input exists. Returns true if available.
• hasNextBigInteger() → Checks if the next token is a BigInteger. Returns true if valid.
• hasNextBigDecimal() → Checks if the next token is a BigDecimal. Returns true if valid.
2. Methods to Read Input (next Methods)
• next() → Reads the next word from input. Stops at spaces or newlines.
• nextInt() → Reads an integer from input. Stops at spaces or newlines.
• nextLong() → Reads a long integer from input. Stops at spaces or newlines.
• nextDouble() → Reads a decimal value from input. Stops at spaces or newlines.
• nextFloat() → Reads a floating-point number. Stops at spaces or newlines.
• nextShort() → Reads a short integer from input. Stops at spaces or newlines.
• nextByte() → Reads a byte value from input. Stops at spaces or newlines.
• nextBoolean() → Reads a boolean value (true/false). Stops at spaces or newlines.
• nextLine() → Reads an entire line as a string. Stops at the newline character.
• nextBigInteger() → Reads a large integer. Stops at spaces or newlines.
• nextBigDecimal() → Reads a large decimal number. Stops at spaces or newlines.
3. Methods for Input Source and Buffer Control
• close() → Closes the scanner to free system resources. Should be called when done.
• reset() → Resets the scanner's internal settings. Returns it to its initial state.
• ioException() → Returns any input errors that occurred. Helps with error handling.
4. Methods to Set and Get Delimiters
• useDelimiter(String pattern) → Changes the separator used for tokens. Default is spaces.
• delimiter() → Returns the current delimiter pattern. Shows how input is split.
5. Methods for Locale and Radix Handling
• useLocale(Locale locale) → Sets the locale for number formatting. Affects decimal symbols.
• locale() → Returns the current locale setting. Helps with international formats.
• useRadix(int radix) → Sets the number base for conversion. Default is 10 (decimal system).
• radix() → Returns the current radix/base. Determines how numbers are interpreted.
Understanding Tokens in Scanner
A token is a unit of input separated by spaces by default. Tokens can be words or numbers.
Example Input:
"Hello 123 45.67"
• next() → Reads "Hello", stopping at the first space.
• nextInt() → Reads 123, stopping at the next space.
• nextDouble() → Reads 45.67, stopping at the newline.
Changing Token Separation
By default, Scanner splits input by spaces. You can change this using useDelimiter():
• useDelimiter(",") → Sets commas as separators instead of spaces.
Additional Notes
• BigInteger & BigDecimal help handle extremely large numbers.
• useLocale() ensures numbers match country-specific formats.
• useDelimiter() allows flexible input separation for easier data processing.