Take a char input from the Scanner

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

    Try this: char c=S.nextLine().charAt(0);

    0 讨论(0)
  • 2020-11-22 05:49

    you just need to write this for getting value in char type.

    char c = reader.next().charAt(0);
    
    0 讨论(0)
  • 2020-11-22 05:53

    Just use...

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

    This gets the first character of the next input.

    0 讨论(0)
  • 2020-11-22 05:53

    To find the index of a character in a given sting, you can use this code:

    package stringmethodindexof;
    
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author ASUS//VERY VERY IMPORTANT
     */
    public class StringMethodIndexOf {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            String email;
            String any;
            //char any;
    
    //any=JOptionPane.showInputDialog(null,"Enter any character or string to find out its INDEX NUMBER").charAt(0);       
    //THE AVOBE LINE IS FOR CHARACTER INPUT LOL
    //System.out.println("Enter any character or string to find out its INDEX NUMBER");
           //Scanner r=new Scanner(System.in);
          // any=r.nextChar();
            email = JOptionPane.showInputDialog(null,"Enter any string or anything you want:");
             any=JOptionPane.showInputDialog(null,"Enter any character or string to find out its INDEX NUMBER");
            int result;
            result=email.indexOf(any);
            JOptionPane.showMessageDialog(null, result);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:54

    You can solve this problem, of "grabbing keyboard input one char at a time" very simply. Without having to use a Scanner all and also not clearing the input buffer as a side effect, by using this.

    char c = (char)System.in.read();
    

    If all you need is the same functionality as the C language "getChar()" function then this will work great. The Big advantage of the "System.in.read()" is the buffer is not cleared out after each char your grab. So if you still need all the users input you can still get the rest of it from the input buffer. The "char c = scanner.next().charAt(0);" way does grab the char but will clear the buffer.

    // Java program to read character without using Scanner
    public class Main
    {
        public static void main(String[] args)
        {
            try {
                String input = "";
                // Grab the First char, also wait for user input if the buffer is empty.
                // Think of it as working just like getChar() does in C.
                char c = (char)System.in.read();
                while(c != '\n') {
                    //<do your magic you need to do with the char here>
                    input += c; // <my simple magic>
    
                    //then grab the next char
                    c = (char)System.in.read();
                }
                //print back out all the users input
                System.out.println(input);
            } catch (Exception e){
                System.out.println(e);
            }
        }
    }  
    

    Hope this helpful, and good luck! P.S. Sorry i know this is an older post, but i hope that my answer bring new insight and could might help other people who also have this problem.

    0 讨论(0)
  • 2020-11-22 05:56

    Setup scanner:

    reader.useDelimiter("");
    

    After this reader.next() will return a single-character string.

    0 讨论(0)
提交回复
热议问题