Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files, etc to fully execute the I/O operations. There are two ways by which we can take input from the user or from a file.
It is a simple class that is used to read a sequence of characters. It has a simple function that reads a character another read which reads, an array of characters, and a readLine() function which reads a line.
InputStreamReader() is a function that converts the input stream of bytes into a stream of character so that it can be read as BufferedReader expects a stream of character.
BufferedReader can throw checked Exceptions
// Java Program For BufferedReader Class import.java.io.*; public class BufferedReader { // Main Method public static void main(String [] args) { // Creating BufferedReader Object // InputStreamReader converts bytes to // stream of character BufferedReader BufferedReader_Name = new BufferedReader(new InputStreamReader(System.in)); // Asking for input from user System.out.println("Enter String : "); System.out.println("Enter Integer : "); // String reading internally String String_name = BufferedReader_Name.readLine(); // Integer reading internally int Integer_value = Integer.parseInt(BufferedReader_name.readLine()); // Printing String System.out.println("Entered String : "+ String_name); //Printing Integer System.out.println("Entered Integer : "+ Integer_value); } }
It is an advanced version of BufferedReader which was added in later versions of Java. The scanner can read formatted input. It has different functions for different types of data types.
Syntax for Scanner
Scanner Scanner_name = new Scanner(System.in);
Syntax for importing Scanner Class: To use the Scanner we need to import the Scanner Class
import java.util.Scanner ;
Inbuilt Scanner functions are as follows
Hence, in the case of Integer and String in Scanner, we don’t require parsing as we did require in BufferedReader.
// Java Program to show how to take // input from user using Scanner Class import java.util.Scanner; class Scanner { public static void main( String[] args ) { // Scanner definition Scanner Scanner_name= new Scanner(System.in); // input is a string read // by readLine() function String str= str.readLine(); // print string System.out.ptintln("Entered String : "+ str); // input is an Integer // read by nextInt() function int x= Scanner_name.nextInt(); // print integer System.out.ptintln("Entered Integer : "+ x); // input is a floatingValue // read by nextFloat() function float f = Scanner_name.nextFloat(); // print floating value System.out.ptintln("Entered FloatValue : "+ f); } }
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.