Take a char input from the Scanner

前端 未结 22 2283
[愿得一人]
[愿得一人] 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条回答
  •  失恋的感觉
    2020-11-22 06:12

    You could take the first character from Scanner.next:

    char c = reader.next().charAt(0);
    

    To consume exactly one character you could use:

    char c = reader.findInLine(".").charAt(0);
    

    To consume strictly one character you could use:

    char c = reader.next(".").charAt(0);
    

提交回复
热议问题