DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2005
    Posts
    27

    BufferedReader Question

    Does anyone know how to read in a char using BufferedReader

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    it has a read() method which takes in a single character, it returns an int so you'll just have to cast this to a char.

    Code:
    import java.io.*;
    
    public class BufferedReaderTest {
        public static void main ( String args[] ) {
    		BufferedReader in = new BufferedReader( new InputStreamReader( System.in ));
    		
    		try {
    			char c = (char) in.read();
    			System.out.println( c );
    		} catch (IOException ioe) { }
        }
    }
    This will wait for input from the user, then when the user enters a line, it will spit back the first character:
    Code:
    test <-- user input
    t <-- computer response

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    These are classes which work with streams. If you are creating an input stream reading from a file, create an instance of BufferedReader, let's say it is named "in":

    BufferedReader in
    = new BufferedReader(new FileReader("foo.in"));

    If you are using some other input stream, such as from the console,

    BufferedReader in
    = new BufferedReader(new InputStreamReader(System.in));

    The "Reader" class translates the bytes in the stream into characters in the stream, and the BufferedReader is a wrapper class for the stream which enables you to better control the stream.

    To read the first character, now use:

    char myChar = in.read();

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by nspils
    char myChar = in.read();
    That will get an error, the read() method returns an int. You'd have to cast it:
    Code:
    char myChar = (char) in.read();

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Yup ... I didn't go back to the API to refresh my recollection of the return type of the read() method. Thanks for correcting me!

  6. #6
    Join Date
    Dec 2005
    Posts
    10

    Smile http://javainterviewquestions.50webs.com/

    I have compiled all possible questions. Feel free to suggest your own q/a.

Similar Threads

  1. Display one record at a time!
    By ASP learner in forum ASP.NET
    Replies: 5
    Last Post: 10-08-2002, 07:17 PM
  2. question on best programming option
    By James in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 07:14 AM
  3. VB,STORED PROC, COM, DTC QUESTION?
    By melvin ng in forum VB Classic
    Replies: 1
    Last Post: 11-10-2000, 03:46 AM
  4. Replies: 0
    Last Post: 05-03-2000, 02:26 PM
  5. Performance question
    By John in forum VB Classic
    Replies: 2
    Last Post: 04-05-2000, 09:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links