• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.in.read() not returning what is entered  RSS feed

 
Eston Burciaga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very basic program that just regurgitates what was the integer that was input by the user. I am having problems with the "System.in.read()" function.

I call the function and then print the value returned. but the value returned isn't the value that is entered - it is always the value entered + 50. For example, if I enter "3" it will print "53". I tried to see what was going on, so I set the returned value to an known integer (say "9" for example). In that case, it returned the value correctly. What is going on? Am I using the "System.in.read()" function incorrectly? Do I need to initialize the input stream first? Do I need to clear the input stream first?

Here is the code I am using:


 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post: always use them. Doesn't it look better I also think you were short of a final } so I added that.

I don't like the read() method; there are far better ways to read things from System.in. For a start it reads one keystroke at a time, so you have to use it in a loop. If you used something like a Scanner, you could read a whole line all at once.

Just because the character '9' looks like 9 doesn't mean the JVM will interpret it as 9. What happens is that you have a char which is promoted to an int and that is what you get back. If you look in this Unicode chart, you can see what corresponds to '9'. It isn't 9 but 0x0039 (in hex) which comes out as 57 in decimal. At least I think that is its value. There are various ways to convert that char to 9; this being one. [Note: there are two overloded versions of that method which do similar things.]
But I recommend: stop using read().

By the way: that empty catch block is potentially dangerous; you could suffer an Exception and never know anything about it.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you get 53 if you enter '3'? I would expect 51.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to declare input. You can write System.in.read(); which works just as badly
 
Eston Burciaga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richie - You are correct. I get "51" when I enter a "3". So what's going on? Is it the fact the the read() function is not interpreting the integer character correctly as an int, or is it the fact that I am using the InputStream.read() function? if it is the read() function, what other function should I be using?
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already told you: '3' does not equal 3. It equals 51. A char is not a letter; it is a number. It even says so in the Java Language Specification. What you get when you pass a char is the number which it corresponds to in ASCII/Unicode. you should have seen 3 and 3 against the character '3' in the link I gave you earlier. That is 0x0033 (hex) which means 51 in decimal.

I hope I have given you enough information to persuade you to stop using read().
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the Scanner link I showed you?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!