java console input - java

The data type of the any input through console (as i do using BufferedReader class) is String.After that we type cast it to requered data type(as Inter.parseInt() for integer).But in C we can take input of any primitive data type whereas in java all input types are neccerily String.why it is so????

Console input is actually read in as a series of bytes, not a String. This is because System.in is exposed by the API as an InputStream. The typical wrapping before JDK1.5 (hooray for the Scanner class!) was something like:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
i.e. InputStreamReader converts the byte stream into a character stream and then the BufferedReader is used to do your readLine() operations, or whatever.
So it's a String output because you're getting the buffered output of a character stream from your BufferedReader.

In java you can do:
Scanner scan = new Scanner(System.in);
scan.nextInt();
scan.nextDouble();
etc. You just need to make sure the next input is correct.
EDIT: Missed the Buffered Reader part. I think this answer is totally irrevelant.

Related

Difference between using java.io.* and java.util.Scanner [duplicate]

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that the BufferedReader reads files efficiently by using a buffer to avoid physical disk operations.
My questions are:
Does Scanner perform as well as BufferedReader?
Why would you choose Scanner over BufferedReader or vice versa?
Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.
In fact you can pass a BufferedReader to a scanner as the source of characters to parse.
In currently latest JDK 18 release/build (b37), the Scanner has a smaller buffer (1024 chars) as opposed to the BufferedReader (8192 chars), but it's more than sufficient.
As to the choice, use the Scanner if you want to parse the file, use the BufferedReader if you want to read the file line by line. Also see the introductory text of their aforelinked API documentations.
Parsing = interpreting the given input as tokens (parts). It's able to give back you specific parts directly as int, string, decimal, etc. See also all those nextXxx() methods in Scanner class.
Reading = dumb streaming. It keeps giving back you all characters, which you in turn have to manually inspect if you'd like to match or compose something useful. But if you don't need to do that anyway, then reading is sufficient.
See this link, following is quoted from there:
A BufferedReader is a simple class meant to efficiently read from the
underling stream. Generally, each read request made of a Reader like a
FileReader causes a corresponding read request to be made to
underlying stream. Each invocation of read() or readLine() could
cause bytes to be read from the file, converted into characters, and
then returned, which can be very inefficient. Efficiency is improved
appreciably if a Reader is warped in a BufferedReader.
BufferedReader is synchronized, so read operations on a BufferedReader
can safely be done from multiple threads.
A scanner on the other hand has a lot more cheese built into it; it
can do all that a BufferedReader can do and at the same level of
efficiency as well. However, in addition a Scanner can parse the
underlying stream for primitive types and strings using regular
expressions. It can also tokenize the underlying stream with the
delimiter of your choice. It can also do forward scanning of the
underlying stream disregarding the delimiter!
A scanner however is not thread safe, it has to be externally
synchronized.
The choice of using a BufferedReader or a Scanner depends on the code
you are writing, if you are writing a simple log reader Buffered
reader is adequate. However if you are writing an XML parser Scanner
is the more natural choice.
Even while reading the input, if want to accept user input line by
line and say just add it to a file, a BufferedReader is good enough.
On the other hand if you want to accept user input as a command with
multiple options, and then intend to perform different operations
based on the command and options specified, a Scanner will suit
better.
BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream.
Scanner can use tokenize using custom delimiter and parse the stream into primitive types of data, while BufferedReader can only read and store String.
BufferedReader is synchronous while Scanner is not. Use BufferedReader if you're working with multiple threads.
Scanner hides IOException while BufferedReader throws it immediately.
I suggest to use BufferedReader for reading text. Scanner hides IOException while BufferedReader throws it immediately.
The difference between BufferedReader and Scanner are following:
BufferedReader is synchronized but Scanner is not synchronized.
BufferedReader is thread-safe but Scanner is not thread-safe.
BufferedReader has larger buffer memory but Scanner has smaller buffer memory.
BufferedReader is faster but Scanner is slower in execution.
Code to read a line from the console:
BufferedReader:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String st = br.readLine();
// You can make the object InputStreamReader object inside the BufferReader method.
BufferReader br = new BufferedReader(InputStreamReader(System.in));
String st = br.readLine();
// You can even inspect the type of the input stream manually by using Parse method which accepts string parameter.
int x = Integer.parseInt(br.readLine());
// Or you can pass the object directly.
int x = Integer.parseInt(st);
Scanner:
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
The differences between BufferedReader and Scanner are:
BufferedReader reads data, but Scanner parses data.
You can only read String using BufferedReader, using Scanner you can read to different data types like int.
BufferedReader is older than Scanner, it was added on JDK 1.1, while Scanner was added on JDK 5 release.
The buffer size of BufferedReader is larger (8KB) as compared to Scanner's 1KB.
BufferedReader is more suitable for reading files with long String, while Scanner is more suitable for reading small user input from command prompt.
BufferedReader is synchronized, while Scanner is not, which means you cannot share Scanner among multiple threads.
BufferedReader is faster than Scanner because it doesn't spend time on parsing.
BufferedReader is a bit faster as compared to Scanner.
BufferedReader is from java.io package, while Scanner is from java.util package.
On basis of the points we can select our choice.
Thanks for reading!
The Main Differences:
Scanner
Simple text scanner which can parse primitive types and strings using regular expressions.
Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
Example:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
prints the following output:
1
2
red
blue
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i = 1; i <= result.groupCount(); i++) {
System.out.println(result.group(i));
}
s.close();
BufferedReader:
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.
Source used: https://docs.oracle.com
There are different ways of taking input in java like:
1) BufferedReader 2) Scanner 3) Command Line Arguments
BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions.
if you are writing a simple log reader Buffered reader is adequate. if you are writing an XML parser Scanner is the more natural choice.
For more information please refer:
http://java.meritcampus.com/t/240/Bufferedreader?tc=mm69
The answer below is taken from Reading from Console: JAVA Scanner vs BufferedReader
When read an input from console, there are two options exists to achieve that. First using Scanner, another using BufferedReader. Both of them have different characteristics. It means differences how to use it.
Scanner treated given input as token. BufferedReader just read line by line given input as string. Scanner itself provides parsing capabilities just like nextInt(), nextFloat().
But, what is others differences between?
Scanner treated given input as token. BufferedReader as stream line/String.
Scanner tokenized given input using regex. Using BufferedReader must write extra code.
BufferedReader faster than Scanner *point no. 2
Scanner isn’t synchronized, BufferedReader synchronized
Scanner came with since JDK 1.5 and higher.
When should use Scanner, or Buffered Reader?
Look at the main differences between both of them, one using tokenized, others using stream line. When you need parsing capabilities, use Scanner instead. But, I am more comfortable with BufferedReader. When you need to read data from a File, use BufferedReader, because it uses buffer memory when it reads a file, and that reduces physical drive usage. Or you can use BufferedReader as input to Scanner.
I prefer Scanner because it doesn't throw checked exceptions and therefore it's usage results in a more streamlined code.
BufferedReader will probably give you better performance (because Scanner is based on InputStreamReader, look sources). oops, for reading data from files it uses nio. When I tested nio performance against BufferedReader performance for big files nio shows a bit better performance.
For reading data from a file try Apache Commons IO.

Java.io Two ways to obtain buffered character stream from unbuffered byte one

I am switching to Java from c++ and now going through some of the documentation on Java IO. So if I want to make buffered character stream from unbuffered byte stream, I can do this in two ways:
Reader input1 = new BufferedReader(new InputStreamReader(new FileInputStream("Xanadu.txt")));
and
Reader input2 = new InputStreamReader(new BufferedInputStream(new FileInputStream("Xanadu.txt")));
So I can make it character and after this buffered or vise versa.
What is the difference between them and which is better?
Functionally, there is no difference. The two versions will behave the same way.
There is a likely to be difference in performance, with the first version likely to be a bit faster than the second version when you read characters from the Reader one at a time.
In the first version, an entire buffer full of data will be converted from bytes to chars in a single operation. Then each read() call on the Reader will fetch a character directly from the character buffer.
In the second version, each read() call on the Reader performs one or more read() calls on the input stream and converts only those bytes read to a character.
If I was going to implement this (precise) functionality, I would do it like this:
Reader input = new BufferedReader(new FileReader("Xanadu.txt"));
and let FileReader deal with the bytes-to-characters decoding under the hood.
There is a case for using an InputStreamReader, but only if you need to specify the character set for the bytes-to-characters conversion explicitly.

BufferedReader and InputStreamReader in Java

I recently started with Java and want to understand a java module of a large app. I came across this line of java code:
String line = (new BufferedReader(new InputStreamReader(System.in))).readLine();
What does this java code do. Is there a C/C++ equivalent of this?
System.in is the standard input.
InputStreamReader allows you to associate a stream that reads from the specified input (in this case the standard input), so now we have a stream.
BufferedReader is an "abstraction" to help you to work with streams. For example, it implements readLine instead of reading character by character until you find a '\n' to get the whole line. It just returns a String after this proccess.
So this line means: "Read a line from standard input and store it in line variable".
> What does this java code do:
String line is your string object
new BufferedReader().readLine() is the instance of a BufferedReader to read text from a character input stream; and readline() is a method it implements to read until a newline character.
new InputStreamReader() gives you a instance of an InputStreamReader which is the "bridge" between the standard in byte stream and the character stream which a BufferedReader wants.
System.in is the standard input (byte stream)
> Is there a C/C++ equivalent of this
Well... there's no language called C/C++... ;)
So I'll assume you wanted an answer for each of them.
In C, there are no "strings" you have to use a character array, but you can read data in to a character array from stdin with something like:
char input[100];
...
scanf("%99[^\n]", input);
or
fgets (input, 100 , stdin)
In C++, you'd use:
using namespace std;
string line;
getline(cin, line);
Your snippet uses a BufferedReader, chained to an InputStreamReader, to read aline from the standard input console and store it to the String line .
BufferedReader
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.
BufferedReader#readLine()
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
InputStreamReader
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.
System
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined "properties"; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
System.in
The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.
What the code does is just simply read a line from input stream. from pattern point of view, this is a decorator. As to using BufferedReader is aiming to improve IO performance.
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.
For top efficiency, we consider wrapping an InputStreamReader within a BufferedReader. For example:
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));

Performance measure of BufferedReader Vs BufferedInputStream

Recently I have modified my code to
While taking input form STDIN, I moved from Scanner to BufferedInputStream.
I also read about BufferedReader which takes the input from any InputStreamReader. This InputStreamReader can be used with System.in to take STDIN input.
BufferedInputStream has read() method, which further needs to be parsed according to the objective.
In my case first i need to take an integer (let say n) as input from STDIN after that a for loop will take n strings as input. These strings have at max 1,00,000 characters.
Question is : Which one among Scanner, BufferedInputStream and BufferedReader performs better for my objective?
Scanner was designed to simplify acceptance of input parameters at run time from user. This is the java equivalent of scanf()/getc()/cin. The 'Reader's are used to read character data, 'Stream's for streamed data. Scanner is best suited for your purpose. As it is simple to code and use.
I would use a BufferedReader in your case. It will be much faster than Scanner as your strings have quite a lot of characters.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//To get an integer
int N = Integer.parseInt(br.readLine());
//To get a string.
String line = br.readLine()

Scanner vs. BufferedReader

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that the BufferedReader reads files efficiently by using a buffer to avoid physical disk operations.
My questions are:
Does Scanner perform as well as BufferedReader?
Why would you choose Scanner over BufferedReader or vice versa?
Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.
In fact you can pass a BufferedReader to a scanner as the source of characters to parse.
In currently latest JDK 18 release/build (b37), the Scanner has a smaller buffer (1024 chars) as opposed to the BufferedReader (8192 chars), but it's more than sufficient.
As to the choice, use the Scanner if you want to parse the file, use the BufferedReader if you want to read the file line by line. Also see the introductory text of their aforelinked API documentations.
Parsing = interpreting the given input as tokens (parts). It's able to give back you specific parts directly as int, string, decimal, etc. See also all those nextXxx() methods in Scanner class.
Reading = dumb streaming. It keeps giving back you all characters, which you in turn have to manually inspect if you'd like to match or compose something useful. But if you don't need to do that anyway, then reading is sufficient.
See this link, following is quoted from there:
A BufferedReader is a simple class meant to efficiently read from the
underling stream. Generally, each read request made of a Reader like a
FileReader causes a corresponding read request to be made to
underlying stream. Each invocation of read() or readLine() could
cause bytes to be read from the file, converted into characters, and
then returned, which can be very inefficient. Efficiency is improved
appreciably if a Reader is warped in a BufferedReader.
BufferedReader is synchronized, so read operations on a BufferedReader
can safely be done from multiple threads.
A scanner on the other hand has a lot more cheese built into it; it
can do all that a BufferedReader can do and at the same level of
efficiency as well. However, in addition a Scanner can parse the
underlying stream for primitive types and strings using regular
expressions. It can also tokenize the underlying stream with the
delimiter of your choice. It can also do forward scanning of the
underlying stream disregarding the delimiter!
A scanner however is not thread safe, it has to be externally
synchronized.
The choice of using a BufferedReader or a Scanner depends on the code
you are writing, if you are writing a simple log reader Buffered
reader is adequate. However if you are writing an XML parser Scanner
is the more natural choice.
Even while reading the input, if want to accept user input line by
line and say just add it to a file, a BufferedReader is good enough.
On the other hand if you want to accept user input as a command with
multiple options, and then intend to perform different operations
based on the command and options specified, a Scanner will suit
better.
BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream.
Scanner can use tokenize using custom delimiter and parse the stream into primitive types of data, while BufferedReader can only read and store String.
BufferedReader is synchronous while Scanner is not. Use BufferedReader if you're working with multiple threads.
Scanner hides IOException while BufferedReader throws it immediately.
I suggest to use BufferedReader for reading text. Scanner hides IOException while BufferedReader throws it immediately.
The difference between BufferedReader and Scanner are following:
BufferedReader is synchronized but Scanner is not synchronized.
BufferedReader is thread-safe but Scanner is not thread-safe.
BufferedReader has larger buffer memory but Scanner has smaller buffer memory.
BufferedReader is faster but Scanner is slower in execution.
Code to read a line from the console:
BufferedReader:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String st = br.readLine();
// You can make the object InputStreamReader object inside the BufferReader method.
BufferReader br = new BufferedReader(InputStreamReader(System.in));
String st = br.readLine();
// You can even inspect the type of the input stream manually by using Parse method which accepts string parameter.
int x = Integer.parseInt(br.readLine());
// Or you can pass the object directly.
int x = Integer.parseInt(st);
Scanner:
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
The differences between BufferedReader and Scanner are:
BufferedReader reads data, but Scanner parses data.
You can only read String using BufferedReader, using Scanner you can read to different data types like int.
BufferedReader is older than Scanner, it was added on JDK 1.1, while Scanner was added on JDK 5 release.
The buffer size of BufferedReader is larger (8KB) as compared to Scanner's 1KB.
BufferedReader is more suitable for reading files with long String, while Scanner is more suitable for reading small user input from command prompt.
BufferedReader is synchronized, while Scanner is not, which means you cannot share Scanner among multiple threads.
BufferedReader is faster than Scanner because it doesn't spend time on parsing.
BufferedReader is a bit faster as compared to Scanner.
BufferedReader is from java.io package, while Scanner is from java.util package.
On basis of the points we can select our choice.
Thanks for reading!
The Main Differences:
Scanner
Simple text scanner which can parse primitive types and strings using regular expressions.
Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
Example:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
prints the following output:
1
2
red
blue
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i = 1; i <= result.groupCount(); i++) {
System.out.println(result.group(i));
}
s.close();
BufferedReader:
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.
Source used: https://docs.oracle.com
There are different ways of taking input in java like:
1) BufferedReader 2) Scanner 3) Command Line Arguments
BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions.
if you are writing a simple log reader Buffered reader is adequate. if you are writing an XML parser Scanner is the more natural choice.
For more information please refer:
http://java.meritcampus.com/t/240/Bufferedreader?tc=mm69
The answer below is taken from Reading from Console: JAVA Scanner vs BufferedReader
When read an input from console, there are two options exists to achieve that. First using Scanner, another using BufferedReader. Both of them have different characteristics. It means differences how to use it.
Scanner treated given input as token. BufferedReader just read line by line given input as string. Scanner itself provides parsing capabilities just like nextInt(), nextFloat().
But, what is others differences between?
Scanner treated given input as token. BufferedReader as stream line/String.
Scanner tokenized given input using regex. Using BufferedReader must write extra code.
BufferedReader faster than Scanner *point no. 2
Scanner isn’t synchronized, BufferedReader synchronized
Scanner came with since JDK 1.5 and higher.
When should use Scanner, or Buffered Reader?
Look at the main differences between both of them, one using tokenized, others using stream line. When you need parsing capabilities, use Scanner instead. But, I am more comfortable with BufferedReader. When you need to read data from a File, use BufferedReader, because it uses buffer memory when it reads a file, and that reduces physical drive usage. Or you can use BufferedReader as input to Scanner.
I prefer Scanner because it doesn't throw checked exceptions and therefore it's usage results in a more streamlined code.
BufferedReader will probably give you better performance (because Scanner is based on InputStreamReader, look sources). oops, for reading data from files it uses nio. When I tested nio performance against BufferedReader performance for big files nio shows a bit better performance.
For reading data from a file try Apache Commons IO.

Categories