3 ways to take inputs in java (Scanner , console , BufferedReader )

import java.util.*;

import java.io.Console;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;



class Input
{
 public static void main(String []args) throws IOException
 {
  Scanner a = new Scanner(System.in); // creating object of scanner class
  String s=a.nextLine();              // way to take input through Scanner(nextLine for String)
  System.out.println(s);
  int i=a.nextInt();                  // way to take input through Scanner(nextInt for int)
  System.out.println(i);


  String str=System.console().readLine();  // way to take input from console
  System.out.println(str);



  BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); // creating object of BufferedReader
  String str1 = in.readLine();      
  System.out.println(str1);
  int num = Integer.parseInt(in.readLine());
  System.out.println(num);

 }
}

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.