Skip to content
Related Articles

Related Articles

Difference Between Scanner and BufferedReader Class in Java

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 08 Apr, 2022

In Java, Scanner and BufferedReader class are sources that serve as ways of reading inputs. Scanner class is a simple text scanner that can parse primitive types and strings. It internally uses regular expressions to read different types while on the other hand BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of the sequence of characters

The eccentric difference lies in reading different ways of taking input via the next() method that is justified in the below programs over the similar input set.

Example 1:

Java




// Java Program to Illustrate Scanner Class
 
// Importing Scanner class from
// java.util package
import java.util.Scanner;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating object of Scanner class to
        // read input from keyboard
        Scanner scn = new Scanner(System.in);
 
        System.out.println("Enter an integer");
 
        // Using nextInt() to parse integer values
        int a = scn.nextInt();
 
        System.out.println("Enter a String");
 
        // Using nextLine() to parse string values
        String b = scn.nextLine();
 
        // Display name and age entered above
        System.out.printf("You have entered:- " + a + " "
                          + "and name as " + b);
    }
}


Output:

Let us try the same using Buffer class and same Input below as follows: 

Example 2:

Java




// Java Program to Illustrate BufferedReader Class
 
// Importing required class
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
        throws IOException
    {
 
        // Creating object of class inside main() method
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
 
        System.out.println("Enter an integer");
 
        // Taking integer input
        int a = Integer.parseInt(br.readLine());
 
        System.out.println("Enter a String");
 
        String b = br.readLine();
 
        // Printing input entities above
        System.out.printf("You have entered:- " + a
                          + " and name as " + b);
    }
}


Output:

Outputs explanation: In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() does not read values from console and cursor will not come into console it will skip that step. The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next().
In BufferReader class there is no such type of problem. This problem occurs only for the Scanner class, due to nextXXX() methods ignoring newline character and nextLine() only reads till the first newline character. If we use one more call of nextLine() method between nextXXX() and nextLine(), then this problem will not occur because nextLine() will consume the newline character. 

Tip: See this for the corrected program. This problem is same as scanf() followed by gets() in C/C++. This problem can also be solved by using next() instead of nextLine() for taking input of strings as shown here.

Following are the Major Differences between Scanner and BufferedReader Class in Java

  • BufferedReader is synchronous while Scanner is not. BufferedReader should be used if we are working with multiple threads.
  • BufferedReader has a significantly larger buffer memory than Scanner.
  • The Scanner has a little buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer), but it’s more than enough.
  • BufferedReader is a bit faster as compared to scanner because the scanner does the parsing of input data and BufferedReader simply reads a sequence of characters.

This article is contributed by Pranav Adarsh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!