Results 1 to 6 of 6
  1. #1
    ankurdh is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default System.in.read() confusion

    hello all.. i'm new to the community and as well to java. I hav good programming experience wit C and C++. the object oriented concepts wern a prob for me to understand. But i'm stuck wit the basic readin of a number from the keyboard!!!
    i used the System.in.read(); statement. It read the number and returned the ascii code of the number. so for every System.in.read() shud a -48 be appended in the code???
    that would make the task more complicated. so i wanted to know wot other ways are used in java to read numbers from the keyboard?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    If you were to read the API docs (JDK 6 Documentation) that method reads a byte. And since the input stream being read is, obviously, a "text" stream, that byte will be, of course, the character's byte value in the encoding that console is currently using (which may, or may not, be a standard ASCII value). Use the
    Java Code:
    new String(byte[])
    constructor after reading and appending all the bytes you wish to read (or you could use the
    Java Code:
    read(byte[], offset, length)
    method to read multiple bytes), then use the
    Java Code:
    Integer.valueOf(String)
    method to get the actual integer value.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Of course, you can always wrap that input stream in an InputStreamReader and read the "string" directly (using readLine), rather than having to read the bytes and reconstruct the String, but the principle remains the same.

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Its much better style to use InputStreamReader or BufferedInputReader and then read a line of text. Then call a parse function to convert the text to whatever you want.

    Dealing with bytes and manually converting bytes to characters is (1) ugly C style and (2) makes it nearly impossible to properly handle international languages and character sets.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    And, for this sort of Stream, that is true, but not always. Only for "text" Streams, of course, and if the source of the Stream uses a different encoding than the systems default, don't forget to use one of the constructors that allows you to specify the encoding.

    You definately do not want to use a Reader, of any kind, for, say, Image data, obviously. ;-)

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by masijade View Post
    You definately do not want to use a Reader, of any kind, for, say, Image data, obviously. ;-)
    Obviously. You have to know what kind of data is in the file in order to properly process it.

    But if its character data, you want to use a buffered, line-oriented steam.

    And unless you are a guru, in which case you should not be posting in the "new to java" forum, you should not be reading binary, rather use a library that does it all properly.

    Java has libraries for nearly everything. music, audio, images, movies, etc. Do not reinvent the wheel. Go for code reuse.

Similar Threads

  1. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 12:17 PM
  2. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  3. Please Read!!!
    By jeffranc in forum New To Java
    Replies: 0
    Last Post: 08-21-2008, 09:47 PM
  4. System.in.read(); strange error!
    By kantze in forum New To Java
    Replies: 2
    Last Post: 03-19-2008, 02:44 PM
  5. How to read the following
    By rrp in forum New To Java
    Replies: 0
    Last Post: 12-03-2007, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •