Java Scanner Class

Gautam Gautam Donate Nov 27, 2019 · 2 mins read
Java Scanner Class

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings.

The Java Scanner class breaks the input into tokens using a delimiter that is whitespace by default. It provides many methods to read and parse various primitive values.

Java Scanner class is widely used to parse text for string and primitive types using a regular expression.

Java Scanner class extends Object class and implements Iterator and Closeable interfaces.

Commonly used methods of Scanner class

There is a list of commonly used Scanner class methods:

Method Description
public String next() it returns the next token from the scanner.
public String nextLine() it moves the scanner position to the next line and returns the value as a string.
public byte nextByte() it scans the next token as a byte.
public short nextShort() it scans the next token as a short value.
public int nextInt() it scans the next token as an int value.
public long nextLong() it scans the next token as a long value.
public float nextFloat() it scans the next token as a float value.
public double nextDouble() it scans the next token as a double value.

Program - 

Following program demonstrates the use of Scanner class -

 * Program to demonstrate use of Scanner  

import java.util.Scanner;  
public class ScannerTest {

    public static void main(String args[]) {
    	Scanner sc = new Scanner(;  
    	System.out.println("Enter your First Name: "); 
    	String fname =;  
    	System.out.println("Enter your Last Name: "); 
    	String lname =;
    	System.out.println("Enter your Age: ");  
    	int age = sc.nextInt();  
    	System.out.println("Full Name: " + fname +" "+ lname + " Age: " +age);  

Output - 

Enter your First Name: 
Enter your Last Name: 
Enter your Age: 
Full Name: Tom Albright Age: 21



Join Newsletter
Get the latest news right in your inbox. We never spam!
Written by Gautam Sharma Profile
Developer, Writer, Emotional, loves to help, blogger

Connect with Gautam