Take a char input from the Scanner

前端 未结 22 2244
[愿得一人]
[愿得一人] 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 should use your custom input reader for faster results instead of extracting first character from reading String. Link for Custom ScanReader and explanation: https://gist.github.com/nik1010/5a90fa43399c539bb817069a14c3c5a8

    Code for scanning Char :

    BufferedInputStream br=new BufferedInputStream(System.in);
    char a= (char)br.read();
    
    0 讨论(0)
  • 2020-11-22 06:13
    // Use a BufferedReader to read characters from the console.
    import java.io.*;
    class BRRead {
    public static void main(String args[]) throws IOException
    {
    char c;
    BufferedReader br = new
    BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter characters, 'q' to quit.");
    // read characters
    do {
    c = (char) br.read();
    System.out.println(c);
    } while(c != 'q');
    }
    }
    
    0 讨论(0)
  • 2020-11-22 06:14
    import java.io.*;
    
    class abc // enter class name (here abc is class name)
    {
        public static void main(String arg[])
        throws IOException // can also use Exception
        {
            BufferedReader z =
                new BufferedReader(new InputStreamReader(System.in));
    
            char ch = (char) z.read();
        } // PSVM
    } // class
    
    0 讨论(0)
  • 2020-11-22 06:14

    Try this

    Scanner scanner=new Scanner(System.in);
    String s=scanner.next();
    char c=s.charAt(0);
    
    0 讨论(0)
提交回复
热议问题