Reading a single char in Java

后端 未结 8 787
暗喜
暗喜 2020-12-01 18:17

How can a char be entered in Java from keyboard?

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 18:37

    You can use Scanner like so:

    Scanner s= new Scanner(System.in);
    char x = s.next().charAt(0);
    

    By using the charAt function you are able to get the value of the first char without using external casting.

提交回复
热议问题