0

How to input a character in Java?

I want to use switch case with alphabets. So how do I get a user input of a single character using Scanner.

7th Jun 2017, 1:45 PM
Vidul Goenka
Vidul Goenka - avatar
3 Réponses
+ 17
As with the Scanner class, this way : char c = input.next(".").charAt(0); or char c = input.next().charAt(0);
7th Jun 2017, 1:52 PM
Dev
Dev - avatar
+ 17
You initiated the object but not the data type. This would take String as it's data type, not a char. Char is not available as an input in the whole Scanner class, that's why we put a charAt(); after the input.
7th Jun 2017, 1:59 PM
Dev
Dev - avatar
+ 1
sorry @Dayve...my code works for a single word not a character.
7th Jun 2017, 1:59 PM
Vaibhav Tandon
Vaibhav Tandon - avatar