Results 1 to 10 of 10

Thread: Reading char

  1. #1
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Reading char

    Greetings,

    I've been trying to take char inputs and store them in an array but I am unable to do so.Please check have I made any errors in my code
    Java Code:
    import java.io.*;
    public class letter {
        int tries;
        char input[] = new char[6];
        public  void input_letter() throws IOException
        {
            BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
            for (tries=0;tries < 6;tries++)
            {
                System.out.println("Guess");
                input[tries]=(char)br.read();
            }
    
        }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Reading char

    No errrors, looks good. Otherwise you had posted a stack trace or compiler errors, or something that doesn't work.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Reading char

    EDIT:

    crud, misread the code. Ignore my reply.

  4. #4
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Reading char

    Quote Originally Posted by PhHein View Post
    No errrors, looks good. Otherwise you had posted a stack trace or compiler errors, or something that doesn't work.
    I know there are no errors but still it wont work as intended.
    See the output
    Java Code:
    Guess
    a
    Guess
    Guess
    a
    Guess
    Guess
    a
    Guess
    Whereas it should be like taking inputs simultaneously.

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

    Default Re: Reading char

    If you're reading single chars, you're also reading the end of line characters (\r and \n on a MS Windows machine).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Reading char

    Quote Originally Posted by JosAH View Post
    If you're reading single chars, you're also reading the end of line characters (\r and \n on a MS Windows machine).

    kind regards,

    Jos
    That may require some illustration though. Say the user types in 'hi<enter>'. Your application will then read characters like this when you use the read() method:

    read() -> 'h'
    read() -> 'i'
    read() -> '\r' (carriage return)
    read() -> '\n' (line feed)

    So four characters in total requiring four calls to read(), on Windows at least.

    You may want to make your life easier and use BufferedReader.readLine() in stead, which returns all characters typed in as a single String object, but removes the newline characters for you.

  7. #7
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Reading char

    Quote Originally Posted by gimbal2 View Post
    That may require some illustration though. Say the user types in 'hi<enter>'. Your application will then read characters like this when you use the read() method:

    read() -> 'h'
    read() -> 'i'
    read() -> '\r' (carriage return)
    read() -> '\n' (line feed)

    So four characters in total requiring four calls to read(), on Windows at least.

    You may want to make your life easier and use BufferedReader.readLine() in stead, which returns all characters typed in as a single String object, but removes the newline characters for you.
    Can you please elaborate on that method or suggest a method that would do the specified task for me.You even linkify me some pages to read .

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default Re: Reading char

    Look through the rest of BufferedReader (Java Platform SE 7 ), especially the readLine() method.

  9. #9
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Reading char

    looking at it now :)

  10. #10
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Reading char

    I am actually very sorry to bump this thread again but I found a temporary solution to my problem by doing like this --
    Java Code:
    public  void input_letter() throws IOException
        {
            for (tries=0;tries < 6;tries++)
            {
                System.out.print("Guess ");
                input[tries]=(char)br.read();
                while (input[tries]=='\n')
                    input[tries] = ((char)br.read());
            }
    
        }
    but still is there any way that I can restrict the user from entering more than one letter ?

Similar Threads

  1. Reading char
    By JG4m3r in forum New To Java
    Replies: 1
    Last Post: 04-23-2012, 04:30 PM
  2. problem with reading a text file with special char
    By kishan.java in forum New To Java
    Replies: 1
    Last Post: 04-10-2011, 09:30 AM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  4. JTextPane (reading char and its attributes)
    By Defero in forum New To Java
    Replies: 0
    Last Post: 07-19-2008, 08:09 PM
  5. Reading Char without needing to press enter
    By x0psci in forum New To Java
    Replies: 0
    Last Post: 11-23-2007, 04:28 PM

Tags for this Thread

Posting Permissions

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