DEV Community

Cover image for Scanner Vs BufferedReader
Jeff Odhiambo
Jeff Odhiambo

Posted on

Scanner Vs BufferedReader

Taking Input from User

Most of the program often request for data to be processed from the user and most of the programming language if not all provides a mechanism through which a user can enter data to be processed into the computer.

For instance in python its done through the use of input method e.g

name = input("Enter name >>> ")
Enter fullscreen mode Exit fullscreen mode

In C Programming scanf is used e.g,

char name[20];
printf("[+]Enter your name >>> ");
scanf("%s",&name);
Enter fullscreen mode Exit fullscreen mode

How about Java?

In Java, there are two ways of doing it.
Scanner and BufferedReader class are sources that serve as ways of reading inputs. When using scanner, it doesn't through an exception while when using Buffered reader it throws exceptions.

Sample code for the use of the Two

Scanner

import java.util.Scanner;

public class NameReader{
    public static void main(String[] args){
        int Age;
        Scanner input = new Scanner(System.in);
        System.out.print("[+]Enter your Age >>> ");
        Age = input.nextInt();
        System.out.println("Your Age is "+Age);
    }
}
Enter fullscreen mode Exit fullscreen mode

OutPut:

[+]Enter your Age >>> 23
Your Age is 23

Process finished with exit code 0
Enter fullscreen mode Exit fullscreen mode

Buffered Reader

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AgeReader{
    public static void main(String[] args)throws Exception{
        int Age;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("[+]Enter your Age >>> ");
        Age = Integer.parseInt(br.readLine());
        System.out.println("Hello your age is "+Age);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

[+]Enter your Age >>> 23
Hello your age is 23

Process finished with exit code 0
Enter fullscreen mode Exit fullscreen mode

These are the two main mechanisms through which users can interact with the computer in Java.

Top comments (2)

Collapse
 
smartjeff profile image
Jeff Odhiambo

If they both perform the same task which is to take input from the user, what is the need for the two different methods? Do they have any difference or maybe difference in areas of applications? And which one between them is the best? please help me understand

Collapse
 
mikevv profile image
MikeVV
  • Scanner is a reader for some kind of typed objects like numbers, string, etc and with Scanner you can read data directly to variable of the type you need

    int age = new Scanner(System.in).nextInt();

  • BufferedReader is just a reader object with internal data buffer. It is used here as an alternative as plain reader does not have "readLine" method and as a result cannot read from user input till the end of line without a lot of additional code.

In my practice I have seen Scanner used only for user input, but BufferedReader is more widely used, actually for every 1st file reading/stream reading as without buffer file/stream java is reading data slowly byte by byte or something like this.