+ 4

How to input a character value in java ?

when I am trying to input a character through Scanner... by using next(); function.... it is giving a error regarding conversion... when I did same one through blue J ...it ran absolutely correct...need some solution plz https://code.sololearn.com/c7XSb6qLS02i/?ref=app

16th May 2017, 5:03 PM
Dolu
Dolu - avatar
3 Réponses
+ 5
Just an explanation: sc.next() Grabs the first word up to a white space. So, it will be a string, not a character. You would need to convert it to character, or just take the first character from the word. (See @Dayve's) code for that.
16th May 2017, 6:03 PM
Rrestoring faith
Rrestoring faith - avatar
+ 21
// There's a way: ch = sc.next().charAt(0); EDIT -- "For explaination, refer to @Rrestoring faith's answer!"
16th May 2017, 5:07 PM
Dev
Dev - avatar
+ 3
OK...got it.👍.. thank u everyone
17th May 2017, 8:39 AM
Dolu
Dolu - avatar