Java provides two classes, Scanner and BufferedReader, for reading inputs. Both classes are used to read input from different sources. The Scanner class can read primitive data types and strings, and uses regular expressions to parse different data types. On the other hand, the BufferedReader class reads text from a character-input stream, buffering characters for efficient reading of a sequence of characters.
The main difference between the two classes lies in the way they read inputs using the next() method. Let's see how this works with the same input set in two different examples.
Example 1:
In this example, we'll use the Scanner class to read input from the keyboard.
First, we import the Scanner class from the java.util package. Then, we create an object of the Scanner class to read input from the keyboard. We prompt the user to enter an integer and use the nextInt() method to parse the integer value. Next, we prompt the user to enter a string and use the nextLine() method to parse the string value. Finally, we display the name and age entered by the user.
Example 2:
In this example, we'll use the BufferedReader class to read input from the same source as in Example 1.
First, we import the required class. Then, we create an object of the BufferedReader class inside the main() method to read input from the keyboard. We prompt the user to enter an integer and use the readLine() method to read the integer value as a string, which we then parse using the Integer.parseInt() method. Next, we prompt the user to enter a string and use the readLine() method to read the string value. Finally, we display the name and age entered by the user.
Output:
The output of the programs will be the same. However, there is a difference in how they read inputs. In the Scanner class, if we call nextLine() method after any one of the seven nextXXX() methods, the nextLine() method will skip the input from the console. This is because the nextXXX() methods ignore the newline character, and nextLine() reads till the first newline character. This problem can be solved by using one more call of nextLine() method between nextXXX() and nextLine(), or by using next() instead of nextLine() for taking input of strings.
The major differences between the Scanner and BufferedReader classes are:
- BufferedReader is synchronous while Scanner is not. BufferedReader should be used if we are working with multiple threads.
- BufferedReader has a significantly larger buffer memory than Scanner.
- Scanner has a small buffer (1KB char buffer) as opposed to BufferedReader (8KB byte buffer), but it's more than enough.
- BufferedReader is a bit faster compared to Scanner because the Scanner does the parsing of input data, while BufferedReader simply reads a sequence of characters.
Let's break down the differences between the Scanner and BufferedReader classes in Java:
Synchronization: BufferedReader is synchronous, while Scanner is not. BufferedReader should be used if you are working with multiple threads. This means that if you have multiple threads trying to access the same BufferedReader object, the object will ensure that only one thread accesses it at a time, preventing any synchronization issues.
Buffer size: BufferedReader has a significantly larger buffer memory than Scanner. The buffer is the amount of memory allocated for temporarily storing the data that is being read. The larger the buffer, the more data that can be read at once, improving performance.
Data parsing: Scanner is designed to parse data, while BufferedReader is not. Scanner can parse primitive types and strings, while BufferedReader simply reads a sequence of characters. This makes BufferedReader a bit faster than Scanner.
In the example programs shown, both Scanner and BufferedReader are used to read user input. The Scanner program prompts the user for an integer and a string, then uses the nextInt() and nextLine() methods to read the input. However, there is a problem with the nextLine() method in Scanner. If we call nextLine() after any of the seven nextXXX() methods, then the nextLine() method will not read values from the console and the cursor will not come into the console, it will skip that step. This occurs because the nextXXX() methods ignore the newline character, and nextLine() only reads until the first newline character. To solve this problem, we can use an additional call to the nextLine() method between the nextXXX() method and the nextLine() method, which will consume the newline character.
In the BufferedReader program, we use the BufferedReader class to read the input. The program prompts the user for an integer and a string, then uses the readLine() method to read the input. There is no problem with the readLine() method in BufferedReader, and it will read the input correctly.
In conclusion, both Scanner and BufferedReader classes can be used for reading user input in Java. However, they have some differences in terms of synchronization, buffer size, and data parsing. It's important to understand these differences so you can choose the appropriate class for your needs.