Taking Input from Users in Java | Scanner vs BufferedReader

In this tutorial post, we will study and understand the 2 different ways in which we can take input from users in Java programming. The way we take input in Java is not straight forward like C or C++ programming. Since everything in Java is Objects and Classes, taking input from users via the console also happens through Objects(more specifically their methods).

There are 2 basic inbuilt classes that assist in taking input from users. They are –

  • Scanner Class.
  • BufferedReader Class.
1. Using BufferedReader Class

This is the Java classical method to take input, Introduced in JDK1.0. This method is used by wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the user in the command line.

Advantages:

  • The input is buffered for efficient reading.

Drawback:

  • The wrapping code is hard to remember.
Java BufferedReader Program Example –
// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test 
{
    public static void main(String[] args) throws IOException 
    {
        //Enter data using BufferReader
        BufferedReader reader = 
                   new BufferedReader(new InputStreamReader(System.in));
        
        // Reading data using readLine
        String name = reader.readLine();
 
        // Printing the read line
        System.out.println(name);        
    }
}

Input:

Simple Snippets

Output:

Simple Snippets
2. Using Scanner class

This is probably the most preferred method to take input. The main purpose of the Scanner class (available since Java 1.5) is to parse primitive types and strings using regular expressions, however it is also can be used to read input from the user in the command line.

Advantages:

  • Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
  • Regular expressions can be used to find tokens.

Drawback:

  • The reading methods are not synchronized
Java Scanner Program Example –
// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
public class InputDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
        
        System.out.println("Enter a String");
        Scanner scr = new Scanner(System.in);
        String str =  scr.nextLine();
        System.out.println("the string you entered is: "+str);
    }
    
}

Input:

Simple Snippets

Output:

Simple Snippets
Some more Differences between BufferedReader & Scanner
  • BufferedReader is synchronous while Scanner is not.
  • BufferedReader should be used if we are working with multiple threads.
  • BufferedReader has significantly larger buffer memory than Scanner.
  • The Scanner has a little buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer).
  • BufferedReader is a bit faster as compared to scanner because scanner does parsing of input data and BufferedReader simply reads sequence of characters.
Watch it on YouTube

 

Leave a Reply

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