The java.util.Scanner class is a simple text scanning class that parses basic data types and strings. It essentially uses regular expressions to read different data types.
In order to read character sequences efficiently, the Java.io.BufferedReader class reads text from character input streams and character buffers.
Here are the differences between the two classes:
When nextLine() is used in nextXXX(), what's the problem with using the Scanner class?
Try to guess the output of the following code;
// Code using Scanner Classimport 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:- 50 and name as
Let's try using the BufferReader class and use the same input
// Code using BufferedReader Classimport java.io.*;class Differ{public static void main(String args[])throws IOException{BufferedReader br = new BufferedReader(newInputStreamReader(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:- 50 and name as Geek
In the Scanner class, if we call the nextLine() method after any 7 nextXXX() methods, the nextLine() method cannot read anything from the console, and, this cursor will not enter the console, it will skip this step. The nextXXX() methods are 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 newline character ***, but nextLine() does not ignore it. If we use more than one nextLine() method between the nextXXX() method and the nextLine() method, this problem will not occur; because nextLine() consumes newline characters. You can refer to the correct way to write this program (http://code.geeksforgeeks.org/CErAhD). This problem is the same as the scanf() method in C/C++ (http://www.geeksforgeeks.org/problem-with-scanf-when-there-is-fgetsgetsscanf-after-it/) that follows the gets() method.
Other differences:
•BufferedReader supports synchronization, while Scanner does not. If we deal with multithreaded programs, the BufferedReader should be used.
•BufferedReader has sufficient buffer memory relative to Scanner.
•Scanner has very few buffers (1KB character buffering) relative to BufferedReader (8KB byte buffering), but this is more than enough.
•BufferedReader is a little faster than Scanner because Scanner performs class parsing of input data, while BufferedReader simply reads the sequence of characters.
The above is the difference between the Scanner class and the BufferReader class in Java introduced to you by the editor (very detailed). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!