Java - Input and Output ( Scanner and Buffered Reader )

Since every program have basics needs of input and output for interacting with users and providing custom behavior to the user .

There are two ways for taking input in java -

  1. Scanner Class
  2. BufferedReader Class

1. Scanner Class :-

Scanner class uses token to take input .

Step1 :- Import java.util.Scanner package

import java.util.Scanner;

Step2 :- Initialize Scanner Class object to access method of this class .

Scanner sc = new Scanner(;

Step3 :- Get input and Store it . Here are some common method and their corresponding types :-

  1. sc.nextInt() :- int
  2. sc.nextLong() :- long
  3. sc.nextDouble() :- double
  4. sc.nextFloat() :- float
  5. sc.nextLine() :- String
  6. sc.nextLine().charAt(0) :- char

// Here are some example

2. BufferedReader Class :-

BudderedReader class takes a line by line input .

Step1:- Import BufferedReader and InputStreamReader package .


Step2 :- Initialize BufferedReader Class object to access method of this class .

BufferedReader br = new BufferedReader(new InputStreamReader(;

Step3 :- You will get line as input so you need to parse this to get valid tokens . Here are some common parser methods to get correspoding type :-

  1. Input.parseInt(br.readLine()) :- int
  2. Double.parseDouble(br.readLine()) :- double
  3. Float.parseFloat(br.readLine()) :- float
  4. br.readLine() :- String
  5. br.readLine().charAt(0) :- char
  6. Boolean.parseBoolean(br.readLine()) :- Boolean

Scanner vs BufferedReader

  1. BufferedReader is faster than Scanner .

2. If input is in big paragraph then it is convenient to use BufferedReader over Scanner .

3. You have to write less in case of Scanner .

Thanks …



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vikas Shekhawat

Vikas Shekhawat


Sole Master | Software engineer and a happy 😊 person | Former Irresponsible Boy !!!