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 2 of 2

Thread: How to take a char value in scanner

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default How to take a char value in scanner

    Hello,
    Anyone know how can i take a character value from user in Scanner package? I've been trying by

    char grades;
     
    grades = console.next().charAt(0);

    But it's giving me error.. So, i was wondering if anyone could help me out? Thank You!
    Last edited by helloworld922; May 1st, 2010 at 02:51 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to take a char value in scanner

    You'll have to post more code than that, there's nothing wrong with what you've posted so far, though (assuming that console is an instance of Scanner)

    As a side note, strangely enough, the Scanner class doesn't have a direct method for only reading in one character (you can kind of cheat using the Regex support of Scanner). You can read in a string of characters and only use the first one, but that can be quite cumbersome.

    There are two options:
    1. Use the BufferedReader class, or
    2. Use a regex with the Scanner.

    // read in one character using Scanner with regex
    Scanner reader = new Scanner(System.in);
    reader.next(".").charAt(0); // allows any character
    reader.next("\\d").charAt(0); // a digit
    reader.next("\\D").charAt(0); // a non-digit
    reader.next("\\S").charAt(0); // a non-whitespace character
    reader.next("\\w").charAt(0); // a "word" character (letters and digits)

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    andaji (May 1st, 2010), JavaPF (May 1st, 2010)

Similar Threads

  1. coverting to Char?
    By JavaNoob82 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 16th, 2010, 07:52 AM
  2. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 10:13 PM
  3. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 02:18 PM
  4. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 09:01 AM
  5. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM