Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
420 views
Welcome To Ask or Share your Answers For Others

1 Answer

InputStream is fundamentally a binary construct. If you want to read text data (e.g. from the console) you should use a Reader of some description. To convert an InputStream into a Reader, use InputStreamReader. Then create a BufferedReader around the Reader, and you can read a line using BufferedReader.readLine().

More alternatives:

  • Use a Scanner built round System.in, and call Scanner.nextLine
  • Use a Console (obtained from System.console()) and call Console.readLine

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...