Take a char input from the Scanner

前端 未结 22 2246
[愿得一人]
[愿得一人] 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 05:57

    There is no API method to get a character from the Scanner. You should get the String using scanner.next() and invoke String.charAt(0) method on the returned String.

    Scanner reader = new Scanner(System.in);
    char c = reader.next().charAt(0);
    

    Just to be safe with whitespaces you could also first call trim() on the string to remove any whitespaces.

    Scanner reader = new Scanner(System.in);
    char c = reader.next().trim().charAt(0);
    
    0 讨论(0)
  • 2020-11-22 05:58

    The easiest way is, first change the variable to a String and accept the input as a string. Then you can control based on the input variable with an if-else or switch statement as follows.

    Scanner reader = new Scanner(System.in);
    
    String c = reader.nextLine();
    switch (c) {
        case "a":
            <your code here>
            break;
        case "b":
            <your code here>
            break;
        default: 
            <your code here>
    }
    
    0 讨论(0)
  • 2020-11-22 05:59
    Scanner key = new Scanner(System.in);
    //shortcut way 
    char firstChar=key.next().charAt(0);  
    //how it works;
    /*key.next() takes a String as input then,
    charAt method is applied on that input (String)
    with a parameter of type int (position) that you give to get      
    that char at that position.
    You can simply read it out as: 
    the char at position/index 0 from the input String
    (through the Scanner object key) is stored in var. firstChar (type char) */
    
    //you can also do it in a bit elabortive manner to understand how it exactly works
    String input=key.next();  // you can also write key.nextLine to take a String with spaces also
    char firstChar=input.charAt(0);
    char charAtAnyPos= input.charAt(pos);  // in pos you enter that index from where you want to get the char from
    

    By the way, you can't take a char directly as an input. As you can see above, a String is first taken then the charAt(0); is found and stored

    0 讨论(0)
  • 2020-11-22 06:02

    There are two approaches, you can either take exactly one character or strictly one character. When you use exactly, the reader will take only the first character, irrespective of how many characters you input.

    For example:

    import java.util.Scanner;  
    
    public class ReaderExample {  
    
        public static void main(String[] args) {  
    
            try {  
    
            Scanner reader = new Scanner(System.in);
    
            char c = reader.findInLine(".").charAt(0);
    
                reader.close();  
    
                System.out.print(c);
    
            } catch (Exception ex) {  
    
                System.out.println(ex.getMessage());  
    
            }
    
    
    
        }  
    
    }  
    

    When you give a set of characters as input, say "abcd", the reader will consider only the first character i.e., the letter 'a'

    But when you use strictly, the input should be just one character. If the input is more than one character, then the reader will not take the input

    import java.util.Scanner;  
    
    public class ReaderExample {  
    
        public static void main(String[] args) {  
    
            try {  
    
            Scanner reader = new Scanner(System.in);
    
            char c = reader.next(".").charAt(0);
    
                reader.close();  
    
                System.out.print(c);
    
            } catch (Exception ex) {  
    
                System.out.println(ex.getMessage());  
    
            }
    
    
    
        }  
    
    }  
    

    Suppose you give input "abcd", no input is taken, and the variable c will have Null value.

    0 讨论(0)
  • 2020-11-22 06:03

    The best way to take input of a character in Scanner class is:

    Scanner sca=new Scanner(System.in);
    System.out.println("enter a character");
    char ch=sca.next().charAt(0);
    
    0 讨论(0)
  • 2020-11-22 06:04

    You should get the String using scanner.next() and invoke String.charAt(0) method on the returned String.
    Exmple :

        import java.util.Scanner;
    
        public class InputC{
    
    
                public static void main(String[] args) {
                    // TODO Auto-generated method stub
                       // Declare the object and initialize with
                       // predefined standard input object
                        Scanner scanner = new Scanner(System.in);
                        System.out.println("Enter a character: ");
                        // Character input
                        char c = scanner.next().charAt(0);
                        // Print the read value
                        System.out.println("You have entered: "+c);
                }
    
    
            }
    

    output

    Enter a character: 
    a
    You have entered: a
    
    0 讨论(0)
提交回复
热议问题