Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Difference between BufferedReader and InputStreamReader output for the code below.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difference between BufferedReader and InputStreamReader output for the code below.

    I have following code :
    char ch;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("input character list : ");
    do{
    ch = (char)br.read() ;
    System.out.println(ch);
    }while(ch!='q');
    If input is shashiq , output comes as shashiq

    When I have code as :
    char ch1;
    InputStreamReader isr = new InputStreamReader(System.in);
    System.out.println("input character list 2: ");
    do{
    ch1 = (char)isr.read() ;
    System.out.print(ch1);
    }while(ch!='q');

    If input is shashiq , output comes as s

    Please clarify the difference , and how does loop runs for both codes


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Difference between BufferedReader and InputStreamReader output for the code below.

    Well, for one, you have a spelling error.
    In your second one, this:
    }while(ch!='q');
    should be this:
    }while(ch1!='q');
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between BufferedReader and InputStreamReader output for the code below.

    Thanks ....I have been scratching my head for a while.....
    well please clarify 1 more point for the above code.....As per my view ,the difference in both the code would be related to performance , as the BufferedReader objects reads the next character from the buffer.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Difference between BufferedReader and InputStreamReader output for the code below.

    Your answer is provided in the API documentation for InputStreamReader: InputStreamReader (Java Platform SE 7 )
    Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

    For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:
    BufferedReader in
    = new BufferedReader(new InputStreamReader(System.in));
    And, from the BufferedReader API: BufferedReader (Java Platform SE 7 )
    Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
    So yes. You basically had the idea.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. InputStreamReader blocking threads
    By coffeecups in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2014, 02:25 PM
  2. Replies: 1
    Last Post: February 10th, 2012, 08:49 AM
  3. Format difference in System.out Vs. file output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 09:13 PM
  4. What is the difference between the belows code
    By lulzimfazlija in forum Java Theory & Questions
    Replies: 4
    Last Post: January 21st, 2011, 01:15 PM
  5. How to Read user input from the console with InputStreamReader in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:48 AM

Tags for this Thread