Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

System.in.read()  RSS feed

 
fred bates
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use System.in.read to re trieve int values
from the keyboard I seem to be getting the ASCII
value of the integer, and can't cast it to an int.
What can I do to fix this problem?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're getting ASCII values (or rather, ISO-8859-1 values, most likely) of the characters entered by the user. You can convert the user input into a single String by wrapping System.in in an InputStreamReader, and wrapping that in a BufferedReader (which has a readLine() method). Then you can use Integer.parseInt(String) to convert the String to an int.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!