Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to properly take input from a user  RSS feed

 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm enjoying Herbert Schildt's 6th edition but I guess if I was forced to complain less than halfway through I'd say there is a bit of vagueness in explaining how to properly take input from a user. I'm trying to stick to the methods in the book but looking at Google searches the methods used aren't even explained by the end of Chapter 6. So here is a quick program I typed out which is just a similar way of taking input that's shown in the book. In this method I want the integer value entered by the user but if I enter 3 I'm getting 51. In an earlier chapter this method was used to capture a character for menu selection so I presumed that it would work for an integer as well.



I'm tinkering around with a way to get the integer and create an array with it based on the requirements laid out on page 223.

Thanks for any suggestions.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appears to be returning the ASCII code for the number rather than the number itself...
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This worked...

 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't want to look at the answer in the back of the book but I had to after figuring that part out. It's interesting that the problem says to let the user specify the size of the stack which is what I was doing with an array before pushing it into the stack. However the answer in the back doesn't give the user any options at all.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.in.read()?
Good grief! Avoid such read() methods like the plague. Their only use is to make every other method look so much better. If you look up the documentation for System.in and its read() method, it reads the next byte (0…0xff) of input as an int, which gives you ASCII values if you use ASCII characters only. That is something you never want to use yourself.

Search my posts for what Rob Spoor told me about Scanners and how to stop them throwing Exceptions. Also search my posts for utility class because I think the best way to get keyboard input is to write yourself a utility class which uses a Scanner object. If you look through this recent thread, you will see Kendall Ponder wrote himself a similar utility class. That is slightly different because he was trying to get yes/no rather than numbers. I seem to have written a (partial) utility class here. Go through the documentation for Scanner and it shows you how to read from a file. You usually must close a Scanner, except when it is reading from System.in. You must not close a Scanner/Reader reading from System.in because you will get the same problem you can read about here.
 
Brian Barrick
Ranch Hand
Posts: 67
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:System.in.read()?
Good grief! Avoid such read() methods like the plague. Their only use is to make every other method look so much better. If you look up the documentation for System.in and its read() method, it reads the next byte (0…0xff) of input as an int, which gives you ASCII values if you use ASCII characters only. That is something you never want to use yourself.

Search my posts for what Rob Spoor told me about Scanners and how to stop them throwing Exceptions. Also search my posts for utility class because I think the best way to get keyboard input is to write yourself a utility class which uses a Scanner object. If you look through this recent thread, you will see Kendall Ponder wrote himself a similar utility class. That is slightly different because he was trying to get yes/no rather than numbers. I seem to have written a (partial) utility class here. Go through the documentation for Scanner and it shows you how to read from a file. You usually must close a Scanner, except when it is reading from System.in. You must not close a Scanner/Reader reading from System.in because you will get the same problem you can read about here.


Thanks for the advice and knowledge on the read methods. I wonder why he uses that in the book, simplicity I guess. The answer in the back doesn't even match the requirements though which I find odd.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!