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!
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.
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.
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)