This week's book giveaway is in the Java in General forum.
We're giving away four copies of Java by Comparison (eBook) and have Simon Harrer, Jörg Lenhard, Linus Dietz on-line!
See this thread for details.
Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader problem  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to prompt the user to enter two numbers and then have those numbers set to the two variables cNum and sNum.



The problem is that when I run this when i enter a value the output is a number in the 50's.
Any help is appreciated.
 
Sheriff
Posts: 57797
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to see what the read() method does, and work out why you get 51 when you expect 3. There are other methods in those classes, eg readLine() which are probably more suitable. There is no need to use two BufferedReaders in the same method to read from the same source. Make sure you use a finally block to close the Reader, otherwise you may end up with "resource leaks."

You may find it much easier if you use the Scanner class.
 
Nathan Doe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Campbell Ritchie
Sheriff
Posts: 57797
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have got it worked out then . . . well done
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!