Take a char input from the Scanner

前端 未结 22 2280
[愿得一人]
[愿得一人] 2020-11-22 05:16

I am trying to find a way to take a char input from the keyboard.

I tried using:

Scanner reader = new Scanner(System.in);
char c = reade         


        
22条回答
  •  -上瘾入骨i
    2020-11-22 06:09

    You could use typecasting:

    Scanner sc= new Scanner(System.in);
    char a=(char) sc.next();
    

    This way you will take input in String due to the function 'next()' but then it will be converted into character due to the 'char' mentioned in the brackets.

    This method of conversion of data type by mentioning the destination data type in brackets is called typecating. It works for me, I hope it works for u :)

提交回复
热议问题