banner



How To Get Input In Java

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

  • BufferedReader Class
  • Scanner Class

1. BufferedReader

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.

Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course .

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

import .java.io.*;

public class BufferedReader

{

public static void main(String [] args)

{

BufferedReader BufferedReader_Name = new

BufferedReader( new InputStreamReader(System.in));

System.out.println( "Enter String : " );

System.out.println( "Enter Integer : " );

String String_name = BufferedReader_Name.readLine();

int Integer_value =

Integer.parseInt(BufferedReader_name.readLine());

System.out.println( "Entered String : " + String_name);

System.out.println( "Entered Integer : " + Integer_value);

}

}

Output :

2. Scanner

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.

  • The scanner is much easier to read as we don't have to write throws as there is no exception thrown by it.
  • It was added in later versions of Java
  • It contains predefined functions to read an Integer, Character, and other data types as well.

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

import java.util.Scanner;

class Scanner {

public static void main( String[] args )

{

Scanner Scanner_name= new Scanner(System.in);

String str= str.readLine();

System.out.ptintln( "Entered String : " + str);

int x= Scanner_name.nextInt();

System.out.ptintln( "Entered Integer : " + x);

float f = Scanner_name.nextFloat();

System.out.ptintln( "Entered FloatValue : " + f);

}

}

Output :

Take user input in Java

Differences Between BufferedReader and Scanner

  • BufferedReader is a very basic way to read the input generally used to read the stream of characters. Its gives an edge over Scanner as it is faster than Scanner because Scanner does lots of post-processing for parsing the input; as seen in nextInt(), nextFloat()
  • BufferedReader is more flexible as we can specify the size of stream input to be read. (In general, it is there that BufferedReader reads lager input than Scanner)
  • These two factors come into play when we are reading larger input. In general, the Scanner Class serves the input.
  • BufferedReader is preferred as it is synchronized. While dealing with multiple threads it is preferred.
  • For decent input, easy readability. The Scanner is preferred over BufferedReader.

How To Get Input In Java

Source: https://www.geeksforgeeks.org/how-to-take-input-from-user-in-java/

Posted by: bryantwarajected.blogspot.com

0 Response to "How To Get Input In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel