0

System.in.read() can read a byte and returns it as int(ranging from 0-255) . How do I get the Unicode characters from the user whose integer values are greater than 255?

Edit: I know that this could be solved using Reader, I just want to know if System.in.read() simply CAN NOT read a character(>255) that takes more than a byte to store( because System.in.read() can read only up to a byte)?

ecain
  • 1,282
  • 5
  • 23
  • 54
Shiva Shankari
  • 17
  • 1
  • 1
  • 7
  • Possible duplicate of [Getting unicode values from System.in](https://stackoverflow.com/questions/16050123/getting-unicode-values-from-system-in) – takendarkk Aug 14 '17 at 16:50
  • 1
    Use a Reader (or a Scanner) to read characters. Not a stream. – JB Nizet Aug 14 '17 at 16:50
  • You answered your own question. If a character is encoded as two bytes, there is no way System.in.read() returns these two bytes, since it only returns one byte. A Reader will simply read as many bytes needed from System.in in order to be able to decode the byte(s) to characters. – JB Nizet Aug 14 '17 at 17:02

2 Answers2

0

refer to: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

"...The value byte is returned as an int in the range 0 to 255..."

System.in.read() simply CAN NOT read a character greater than 255.

Charles Knell
  • 342
  • 1
  • 7
-1

enter image description herewell I have just tested the System.in.read() method while using the letter "ת" as an input and it returned 260, so appearantly it can return (or read) characters with bit code that is greater than 255.

          public static void main(String[] args) throws IOException {
           System.out.print("Enter something: ");
           int s = System.in.read();
           System.out.print('\n' + s);
           }

enter image description here

It appears that maybe each computer / machine has it's own char-to-int (which is greater than 255) thing, so each computer may show you a different result for the letter ת that I inputed.

Community
  • 1
  • 1
gever
  • 99
  • 1
  • 10
  • It can't possibly return a value bigger than 255. A byte can't be bigger than 255. The javadoc clearly says that a byte is returned, whose value is in the range 0 to 255. Or -1 if the end of the stream is reached. And of course, when I try your code (MacOS), 225 is being printed. – JB Nizet Aug 14 '17 at 17:43
  • well then try any other special letter on your mac. it says that it read 260 from what I inputed. check out the edit. – gever Aug 14 '17 at 17:46
  • and by the way, System.in.read() does not return a byte. it returns an integer. – gever Aug 14 '17 at 17:48
  • https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read--: Reads the next **byte** of data from the input stream. The value **byte** is returned as an int in the range 0 to 255. If no **byte** is available because the end of the stream has been reached, the value -1 is returned – JB Nizet Aug 14 '17 at 17:50
  • then explain how I got 260. – gever Aug 14 '17 at 17:50
  • I'd guess that it has to to with the output being intermingled with Maven output using ANSI codes for colors, etc. – JB Nizet Aug 14 '17 at 17:53
  • well then, that's pretty much the answer for the question... System.in.read() can read characters greater than byte code 255. It was just proved. – gever Aug 14 '17 at 17:56
  • [link]http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0100 according to this site, the integer value for the character that u entered "ת" is 1514. "Ą" this is the character whose int value is 260 – Shiva Shankari Aug 15 '17 at 05:46
  • so basicly, each machine has it's own char-to-int mechanics and every computer makes it different... so isn't my answer the correct one? – gever Aug 17 '17 at 02:50