The Java.util.Scanner class is a simple text-scanning class that can parse basic data types and strings. It essentially uses regular expressions to read different data types.
The Java.io.BufferedReader class reads text from character input streams and character buffers to enable efficient reading of character sequences.
Here are the differences between the two classes:
What's wrong with scanner class when nextline () is used in nextxxx ()
Try to guess what the output of the following code is;
The Code using Scanner Class
import Java.util.Scanner;
Class Differ
{public
static void Main (String args[])
{
Scanner scn = new Scanner (system.in);
System.out.println ("Enter an Integer");
int a = Scn.nextint ();
System.out.println ("Enter a String");
String B = scn.nextline ();
System.out.printf ("You have entered:-"
+ A + "" + "and name as" + B);
}
Input:
50
Geek
Output:
Enter an integer
Enter a String
You have entered:-and name as
Let's try using the Bufferreader class and use the same input
The Code using BufferedReader Class
import java.io.*;
Class Differ
{public
static void Main (String args[])
throws IOException
{
BufferedReader br = new BufferedReader (New
InputStreamReader (system.in));
System.out.println ("Enter an Integer");
int a = Integer.parseint (Br.readline ());
System.out.println ("Enter a String");
String B = br.readline ();
System.out.printf ("You have entered:-" + A +
"and name as" + B);
}
Input:
50
Geek
Output:
Enter an integer
Enter a String
You have entered:-and name as Geek
In the scanner class if we call the Nextline () method after any of these 7 nextxxx () methods, the Nextline () method cannot read anything from the console, and the cursor does not enter the console, and it skips this step. The Nextxxx () method is these methods, Nextint (), Nextfloat (), Nextbyte (), Nextshort (), nextdouble (), Nextlong (), Next ().
There is no such problem in the Bufferreader class. This problem only occurs in the scanner class, because the Nextxxx () method ignores the * * * Line break * * *, but nextline () does not ignore it. This problem will not occur if we use more than one nextline () method between the Nextxxx () method and the Nextline () method, because Nextline () consumes the line feed. You can refer to the correct wording of this program (Http://code.geeksforgeeks.org/CErAhD). This problem and the scanf () in C + + (http://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/) Method follows the same problem as the gets () method.
Other points of difference:
BufferedReader is supported for synchronization, and scanner is not supported. If we are dealing with multithreaded programs, BufferedReader should use.
BufferedReader has a large buffer memory relative to the scanner.
Scanner has a small buffer (1KB character buffer) relative to the BufferedReader (8KB byte buffer), but this is more than sufficient.
BufferedReader is faster than scanner, because scanner parses the input data, and BufferedReader simply reads the sequence of characters.
The above is a small set to introduce the Java Scanner class and Bufferreader class differences (very detailed), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!