Results 1 to 6 of 6
  1. #1
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default why does java.io.Reader.read() outputs a int, not char?

    as title. im trying to read a url from a InputStream, then i used that read method, but the output of them method is int.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: why does java.io.Reader.read() outputs a int, not char?

    Because it returns -1 to indicate the end of the stream, and chars are unsigned. This was poor API design, but now we're stuck with it.
    Get in the habit of using standard Java naming conventions!

  3. #3
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: why does java.io.Reader.read() outputs a int, not char?

    so basiclly a letter/number/symbol is stored on a int object after read() is used, then we have to convert the object to char?

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: why does java.io.Reader.read() outputs a int, not char?

    Yes. You can do that by casting.

    Java Code:
    int i = myReader.read();
    if(i == -1) {
        // handle end of stream
    } else {
        char c = (char) i;
        // etc.
    }
    Last edited by kjkrum; 11-20-2011 at 04:13 AM. Reason: forgot to close my code tag :-P
    Get in the habit of using standard Java naming conventions!

  5. #5
    fishy8158 is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: why does java.io.Reader.read() outputs a int, not char?

    thanks alot.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: why does java.io.Reader.read() outputs a int, not char?

    I'm certainly no pro at this, but couldn't he wrap his InputStreamReader with a BufferedReader and then call readLine on it to read in a line of text, or null if end of input has occurred? This way no casting is necessary, and since the reader is buffered, there's a potential gain in efficiency.

Similar Threads

  1. Replies: 5
    Last Post: 11-11-2011, 12:51 AM
  2. System.in.read(); to a char array
    By larsjuh in forum New To Java
    Replies: 5
    Last Post: 10-22-2011, 03:26 PM
  3. Line Reader changeing to read 1 line
    By Javanooby in forum New To Java
    Replies: 15
    Last Post: 05-10-2011, 05:34 AM
  4. How to read a Char character ?!
    By Sary in forum New To Java
    Replies: 4
    Last Post: 03-27-2010, 07:01 PM
  5. read char words lines
    By Drukqs in forum New To Java
    Replies: 6
    Last Post: 05-14-2009, 07:14 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
  •