+ 2
What is the statement used to accept a character when using Scanner class?
2 Answers
+ 15
There is no API method to get a character from the Scanner. You should get the String using scanner.next() and invoke String.charAt(0) method on the returned String.
Scanner reader = new Scanner(System.in);
char c = reader.next().charAt(0);
Just to be safe with whitespaces you could also first call trim() on the string to remove any whitespaces.
Scanner reader = new Scanner(System.in);
char c = reader.next().trim().charAt(0);
0
Thank you!