Results 1 to 5 of 5
  1. #1
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default How to read a Char character ?!

    Hello Guys ..
    I just need help in a simple thing ..

    to let the user read a string, we use:
    scan.next() or scan.nextline()

    to let the user read an integer, we use:
    scan.nextInt()

    but what about reading a char ?
    what shall I use ?!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't think you can, not in the way that you are trying (though I could be wrong). To really do this well, you may want to create a Swing GUI and add a DocumentListener to a JTextField.

  3. #3
    Sary is offline Member
    Join Date
    Mar 2010
    Posts
    15
    Rep Power
    0

    Default

    mmm .. got no idea of what u proposed ..
    anyway, thanks a lot dude !

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Java Code:
    Scanner s = new Scanner(System.in);
    char a = s.next().charAt(0);

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

    Default

    Quote Originally Posted by Sary View Post
    Hello Guys ..
    I just need help in a simple thing ..

    to let the user read a string, we use:
    scan.next() or scan.nextline()

    to let the user read an integer, we use:
    scan.nextInt()

    but what about reading a char ?
    what shall I use ?!
    A Scanner is way too 'sophisticated' for reading single chars; better use the wrapped Readers for those simple tasks but if you really want to read a single char with a Scanner you can do it like this:

    Java Code:
    char chr= scanner.next(Pattern.compile(".")).charAt(0);
    Only one char will be read from the input, the rest of the characters will be left in the associated streams.

    kind regards,

    Jos

Similar Threads

  1. Read a single character entered
    By mcmb03 in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 10:30 PM
  2. Replies: 8
    Last Post: 09-15-2009, 11:53 AM
  3. read char words lines
    By Drukqs in forum New To Java
    Replies: 6
    Last Post: 05-14-2009, 06:14 PM
  4. Read from a certain character to a certain character
    By blackstormattack in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 11:36 AM
  5. Char type for first character typed?
    By Sageinquisitor in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 07:08 AM

Posting Permissions

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