π Java Scanner Methods β Complete Guide with Syntax, Examples & Best Practices (2025)
π§² Introduction β Why Java Scanner Methods Are Important
In Java, the Scanner
class is one of the most commonly used tools to take input from users, read files, or parse strings. Whether you’re building a command-line app or processing formatted data β Scanner
provides powerful and flexible methods to read various types of input.
By mastering Java Scanner methods, you’ll be able to:
- β
Read different data types like
int
,double
,String
, and more - β Parse input from files, strings, or user input
- β Handle whitespace and token-based data efficiently
π
Scanner
is part of thejava.util
package.
π What Is the Java Scanner Class?
The Scanner
class allows you to read and parse input from various sources like:
- Keyboard input (
System.in
) - Files (
File
) - Strings (
String
)
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
π Commonly Used Java Scanner Methods
π§© Method | π Purpose |
---|---|
next() | Reads the next word/token |
nextLine() | Reads the entire line |
nextInt() | Reads the next integer |
nextDouble() | Reads the next double |
nextBoolean() | Reads the next boolean |
hasNext() | Checks if thereβs another token |
hasNextInt() | Checks for next int |
hasNextLine() | Checks for next line |
useDelimiter(String) | Changes default delimiter |
close() | Closes the scanner |
β Java Scanner Method Examples
β
1. next()
β Read Next Token (Word)
Scanner sc = new Scanner("Java is fun");
System.out.println(sc.next()); // Output: Java
β Explanation: Reads the next word up to a space or newline.
β
2. nextLine()
β Read the Whole Line
Scanner sc = new Scanner("Hello World!");
System.out.println(sc.nextLine()); // Output: Hello World!
β Explanation: Reads the entire line including spaces.
β
3. nextInt()
β Read Integer
Scanner sc = new Scanner("123");
int num = sc.nextInt();
System.out.println(num); // Output: 123
β Explanation: Parses and returns the next integer.
β
4. nextDouble()
β Read Double
Scanner sc = new Scanner("3.14");
double pi = sc.nextDouble();
System.out.println(pi); // Output: 3.14
β Explanation: Reads a decimal value.
β
5. nextBoolean()
β Read Boolean Value
Scanner sc = new Scanner("true");
boolean b = sc.nextBoolean();
System.out.println(b); // Output: true
β
Explanation: Reads true
or false
values.
β
6. hasNext()
/ hasNextInt()
β Check Availability
Scanner sc = new Scanner("42");
if (sc.hasNextInt()) {
int val = sc.nextInt();
System.out.println("Number: " + val);
}
β Explanation: Verifies the next token is of the expected type.
β
7. useDelimiter()
β Custom Delimiters
Scanner sc = new Scanner("apple,banana,grape");
sc.useDelimiter(",");
while (sc.hasNext()) {
System.out.println(sc.next());
}
Output:
apple
banana
grape
β
Explanation: Reads tokens based on custom delimiter (,
).
β
8. close()
β Free Resources
Scanner sc = new Scanner(System.in);
// read data...
sc.close();
β Always close the scanner to prevent resource leaks.
π Summary Table of Scanner Input Types
π§ Method | π Reads |
---|---|
next() | Next word/token |
nextLine() | Entire line |
nextInt() | Integer value |
nextDouble() | Decimal (float/double) |
nextBoolean() | true/false |
hasNextX() | Checks if value of type exists |
π‘ Best Practices with Scanner
- β
Always check
hasNextX()
before reading input to avoid exceptions. - β
Use
nextLine()
afternextInt()
to consume the newline. - β
Close
Scanner
withclose()
when done (especially with file input). - β οΈ Avoid using multiple
Scanner
instances onSystem.in
.
π§ Summary β Java Scanner Methods
Java Scanner
provides flexible and efficient methods for:
- β Reading different data types from input
- β Parsing lines, words, or numbers
- β Handling console, string, or file input with ease
Itβs the go-to class for beginner-friendly input parsing and essential for text-driven Java apps.
βFAQs on Java Scanner Methods
β What’s the difference between next()
and nextLine()
?
next()
reads up to spacenextLine()
reads the entire line including spaces
β Why does nextLine()
skip input after nextInt()
?
Because nextInt()
leaves the newline (\n
) in the buffer. Use sc.nextLine()
after nextInt()
to consume it.
β How do I read a full sentence with spaces?
Use nextLine()
.
β Can Scanner read from files?
Yes:
Scanner sc = new Scanner(new File("data.txt"));
β Is Scanner thread-safe?
No. It is not synchronized. Use external synchronization if needed.
Share Now :