Java program to take input from user

  • BufferedReader Class
  • Scanner Class

1. BufferedReader

It is an easy class that's wont to read a sequence of characters. it's an easy function that reads a personality another read which reads, an array of characters, and a readLine() function which reads a line.

InputStreamReader() may be a function that converts the input stream of bytes into a stream of character in order that it are often read as BufferedReader expects a stream of character.

BuffferedReader 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);
	
	}
					
}

2. Scanner 

This is an improved version of BufferedReader added in newer versions of Java. The scanner can read formatted input. It has different functions for different types of data.

  • The scanner is much easier to read because we don't have to write a throw because it doesn't throw an exception.
  • It was added in newer Java versions
  • It contains predefined functions for reading integers, symbols, and other types of data.

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

  • Integer: nextInt()
  • Float: nextFloat()
  • String : readLine()

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);
}
}

Differences Between BufferedReader and Scanner

  • BufferedReader is a very simple method for reading input which is usually used to read character streams. It has advantages over scanners in that it is faster than scanners as scanners do a lot of post-processing to parse the input; as seen in nextInt(), nextFloat()
  • BufferedReader is more flexible because we can define the size of the input stream that is read. (In general, BufferedReader reads larger input than scanner)
  • Both of these factors come into play when we read larger inputs. In general, the scanner class is used for input.
  • BufferedReader is preferred because it is synchronized. This is preferable when working with multiple topics.
  • For a decent start, good readability. Scanner is preferred over BufferedReader.

 

Submit Your Programming Assignment Details