Results 1 to 5 of 5
  1. #1
    indianblood is offline Member
    Join Date
    Jan 2012
    Location
    india
    Posts
    2
    Rep Power
    0

    Red face Newbie need help wth BufferedReader

    Ok first of a I am a totally new to java and finding learning java to be Interesting/challenging.
    So i am trying a simple program to accept an int value form the user using BufferedReader method read() . here's the code
    Java Code:
    import java.io.*;
    public class test
    {
      int x=0;
     int getdata() throws Exception
     {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      System.out.println("enter a no");
      x = br.read();
      System.out.println("read :"+x);
      return x;
     }
     
     public static void main (String a[])
     {
      int y=0;
      test t= new test();
      try
      {
      y=t.getdata();
      }catch(Exception e)
       {
       }
      
      System.out.println(y);
     }
    }
    It compile's properly but it dosn't work properly
    for ex if i enter 1 as input i get output 47 :S is it converting the int to ascii or uni-code if so why ?? how to fix it??
    here's ref for bufferedreader that i used link

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Newbie need help wth BufferedReader

    You probably want readLine() and then do a parse on the String returned.
    The value returned by read() is a UTF character (think ASCII and then some).
    47 is the value of the character '1'.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Newbie need help wth BufferedReader

    Quote Originally Posted by Tolls View Post
    47 is the value of the character '1'.
    Throw that computer away; quick!

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    indianblood is offline Member
    Join Date
    Jan 2012
    Location
    india
    Posts
    2
    Rep Power
    0

    Default Re: Newbie need help wth BufferedReader

    Ok i got it :D thank u i seem to have read the reference wrong >.< ..
    i saw :

    int read()
    Read a single character.

    saw the return type Int and thought it meant for reading a single integer no. Now realized that it reads a single char and returns ascii equivalent.

    Yeah found Scanner to accept variables which what i originally wanted.

    Thanks @Tolls for the help :D

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Newbie need help wth BufferedReader

    Quote Originally Posted by JosAH View Post
    Throw that computer away; quick!

    kindest regards,

    Jos ;-)
    Good lord...so it isn't.
    I knew it was in that range, so I just thought "that'll be the codepoint".
    How odd.

Similar Threads

  1. Using BufferedReader
    By hqt in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 05:12 AM
  2. How to use BufferedReader?
    By ProgramBeginner in forum New To Java
    Replies: 4
    Last Post: 10-11-2011, 03:56 PM
  3. use of BufferedReader?please help
    By Cosmos in forum New To Java
    Replies: 11
    Last Post: 07-17-2011, 05:55 PM
  4. BufferedReader, need help!
    By zacharyrod in forum New To Java
    Replies: 10
    Last Post: 11-19-2009, 11:56 AM
  5. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:57 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
  •