
Java Scanner Methods in Java
In Java, the Scanner
class is part of the java.util
package and is used to read input from various sources, such as the keyboard (standard input), files, and strings. It provides methods for reading different types of data, such as strings, integers, floats, and more.
Creating a Scanner Object
To use the Scanner
class, you first need to import the java.util.Scanner
class and create an instance of the Scanner
object. You can then use various methods to read input.
import java.util.Scanner;public class ScannerExample { public static void main(String[] args) { // Create a Scanner object to read input from the console (System.in) Scanner scanner = new Scanner(System.in); // Example usage of Scanner methods }}
Common Methods of the Scanner Class
1. Reading Input of Different Types
Scanner
provides various methods to read different types of input.
nextLine(): Reads an entire line of text.
String name = scanner.nextLine(); // Reads a full line
next(): Reads the next token (word) from the input. It stops at the first whitespace.
String word = scanner.next(); // Reads the next word (token)
nextInt(): Reads the next integer from the input.
int number = scanner.nextInt(); // Reads an integer
nextDouble(): Reads the next double value from the input.
double price = scanner.nextDouble(); // Reads a double
nextFloat(): Reads the next float value from the input.
float weight = scanner.nextFloat(); // Reads a float
nextBoolean(): Reads the next boolean value from the input (
true
orfalse
).boolean isAvailable = scanner.nextBoolean(); // Reads a boolean
nextByte(): Reads the next byte value from the input.
byte b = scanner.nextByte(); // Reads a byte
nextLong(): Reads the next long value from the input.
long bigNumber = scanner.nextLong(); // Reads a long
nextShort(): Reads the next short value from the input.
short s = scanner.nextShort(); // Reads a short
2. Checking Input Availability
These methods help you check whether there is more input available and what type it is.
hasNext(): Returns
true
if there is another token in the input.if (scanner.hasNext()) { String token = scanner.next(); System.out.println(token);}
hasNextLine(): Returns
true
if there is another line of input.if (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line);}
hasNextInt(): Returns
true
if the next token can be interpreted as an integer.if (scanner.hasNextInt()) { int number = scanner.nextInt(); System.out.println(number);}
3. Delimiters
You can change the default delimiter (whitespace) used by the Scanner
. By default, the Scanner
uses whitespace to separate tokens. You can change the delimiter using the useDelimiter()
method.
useDelimiter(String pattern): Sets the delimiter for separating tokens.
scanner.useDelimiter(","); // Use comma as the delimiter
4. Skipping Input
skip(String pattern): Skips input that matches the given regular expression.
scanner.skip("\\s*"); // Skips all whitespace characters
5. Closing the Scanner
It’s important to close the Scanner
object once you're done using it to free up resources.
scanner.close();
Example Usage
import java.util.Scanner;public class ScannerMethodsExample { public static void main(String[] args) { // Create a Scanner object Scanner scanner = new Scanner(System.in); // Example: Reading a String System.out.print("Enter your name: "); String name = scanner.nextLine(); // Example: Reading an integer System.out.print("Enter your age: "); int age = scanner.nextInt(); // Example: Reading a double System.out.print("Enter your height: "); double height = scanner.nextDouble(); // Display the inputs System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Height: " + height); // Close the scanner to free up resources scanner.close(); }}
Output Example:
Enter your name: JohnEnter your age: 25Enter your height: 5.9Name: JohnAge: 25Height: 5.9
Summary of Common Scanner Methods
Method | Description |
---|---|
nextLine() | Reads a whole line of text. |
next() | Reads the next token (word). |
nextInt() | Reads an integer. |
nextDouble() | Reads a double. |
nextFloat() | Reads a float. |
nextBoolean() | Reads a boolean (true or false ). |
hasNext() | Returns true if there’s more input available. |
hasNextLine() | Returns true if there’s another line of input. |
hasNextInt() | Returns true if the next token is an integer. |
useDelimiter() | Sets the delimiter to separate tokens. |
skip() | Skips input that matches the regular expression. |
close() | Closes the scanner and releases resources. |
Let me know if you'd like further examples or clarification on any specific Scanner
methods!