I am on java 6. Using DataInputStream in = new DataInputStream(System.in);
to read user input. When the readLine() is deprecated. What is the work around for reading user value?
DataInputStream in = new DataInputStream(System.in);
int num;
try
{
num = Integer.parseInt(in.readLine()); //this works
num = Integer.parseInt(in); //just in doesnt work.
}
catch(Exception e)
{
}
please explain as it should when the readLine() is deprecated.
Deprecation and the alternatives is usually already explicitly explained in the javadocs. So it would be the first place to look for the answer. For
DataInputStream
you can find it here. ThereadLine()
method is here. Here's an extract of relevance:The character encoding can then be explicitly specified in the constructor of
InputStreamReader
.The
Scanner
which was introduced since Java 1.5 is also a good (and modern) alternative.calling readLine command in scala returns "warning: there was one deprecation warning; re-run with -deprecation for details" message.
You can handle this warning as illustrated below
InputStream
is fundamentally a binary construct. If you want to read text data (e.g. from the console) you should use aReader
of some description. To convert anInputStream
into aReader
, useInputStreamReader
. Then create aBufferedReader
around theReader
, and you can read a line usingBufferedReader.readLine()
.More alternatives:
Scanner
built roundSystem.in
, and callScanner.nextLine
Console
(obtained fromSystem.console()
) and callConsole.readLine
The below doesn't work,
Instead you should use:
readLine()
will read an input of line until line break.