+ 2

What is the statement used to accept a character when using Scanner class?

18th Feb 2018, 11:42 AM
Alok Agarwal
Alok Agarwal - avatar
2 Réponses
+ 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);
18th Feb 2018, 11:44 AM
Abhivarshini Maddala
Abhivarshini Maddala - avatar
0
Thank you!
18th Feb 2018, 5:53 PM
Alok Agarwal
Alok Agarwal - avatar