Skip to content

Java Scanner Class Comprehensive Guide

The Scanner class in Java (java.util.Scanner) is a powerful utility for reading user input from various sources, including keyboard (System.in), files, and strings. It provides methods to parse and retrieve different data types (int, float, String, etc.) from the input stream.

import java.util.Scanner;
// or import all utility classes
import java.util.*;
Scanner scan = new Scanner(System.in); // Reads from standard input (keyboard)
System.out.print("Enter your first name: ");
String firstName = scan.nextLine(); // Reads a line of text
System.out.print("Enter your last name: ");
String lastName = scan.nextLine();
System.out.println("Your Name is: " + firstName + " " + lastName);
MethodDescription
nextInt()Reads an int value
nextFloat()Reads a float value
nextBoolean()Reads a boolean value
nextLine()Reads a line of text
next()Reads a word (until whitespace)
hasNextLine()Checks if another line is available
hasNextInt()Checks if another int is available
reset()Resets the scanner
toString()Returns string representation of the scanner
nextByte()Reads a byte value
nextDouble()Reads a double value
nextShort()Reads a short value
nextLong()Reads a long value
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scan.nextInt();
System.out.print("Enter second number: ");
int num2 = scan.nextInt();
System.out.println("Sum of entered numbers: " + (num1 + num2));
scan.close();
  • next(): Reads input until whitespace (single word).
  • nextLine(): Reads the entire line (including spaces).

Example with next():

Scanner scan = new Scanner(System.in);
System.out.print("Enter your full name: ");
String name = scan.next(); // Only reads first word
System.out.println("Your name: " + name);
scan.close();

Example with nextLine():

Scanner scan = new Scanner(System.in);
System.out.print("Enter your full name: ");
String name = scan.nextLine(); // Reads the whole line
System.out.println("Your name: " + name);
scan.close();

The useDelimiter(String pattern) method allows you to specify a custom delimiter for input parsing.

Example:

Scanner scan = new Scanner("BeginnersBook/Chaitanya/Website");
scan.useDelimiter("/");
while (scan.hasNext()) {
System.out.println(scan.next());
}
scan.close();
  • Always close the Scanner object after use (scan.close()).
  • Be careful when mixing nextInt()/next() and nextLine(); after reading a number, call scan.nextLine() to consume the leftover newline.
  • For file input, use: Scanner scan = new Scanner(new File("filename.txt"));

Summary: The Scanner class is essential for interactive Java programs, supporting robust and flexible input handling for various data types and sources. Use the appropriate method for your data type, and always manage resources by closing the scanner when done.