Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accepting integer or float or double value from keyboard  RSS feed

 
Md.Afzalur Rashid
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to accept number from keyboar.I used System.in.read().By using this I am able to accept the data but the value returned by the method is not correct can you give me any Idea to accept data from keyboard.
thank You
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the best way is to wrap System.in in a Reader and then parse the input with Double.parseDouble. Here's some sample code:

Hope this helps,
Michael Morris
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful - if you create the BufferedReader inside the while loop, it will be susceptible to GC when (if) the loop repeats. This usually means the system will call close() on the BufferedReader, which will close() the InputStreamReader, which will then close() System.in. This will probably end your program, or at least make it impossible to enter any more numbers. I'd declare the BufferedReader as a static class variable, so it won't be accidentally GC'ed while your program is still running.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Careful - if you create the BufferedReader inside the while loop, it will be susceptible to GC when (if) the loop repeats.

Good point. I guess that's why you're wearin' the tin badge and servin' redeye.
Michael Morris
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!