πŸ“š Java Reference
Estimated reading: 3 minutes 29 views

πŸ” 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 the java.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() after nextInt() to consume the newline.
  • βœ… Close Scanner with close() when done (especially with file input).
  • ⚠️ Avoid using multiple Scanner instances on System.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 space
  • nextLine() 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java Scanner Methods

Or Copy Link

CONTENTS
Scroll to Top