Take a char input from the Scanner

2018-12-31 08:04发布

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 = reader.nextChar();

This method doesn't exist.

I tried taking c as a String. Yet, it would not always work in every case, since the other method I am calling from my method requires a char as an input. Therefore I have to find a way to explicitly take a char as an input.

Any help?

20条回答
不再属于我。
2楼-- · 2018-12-31 08:29
// 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');
}
}
查看更多
何处买醉
3楼-- · 2018-12-31 08:29

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
查看更多
明月照影归
4楼-- · 2018-12-31 08:29
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

查看更多
骚的不知所云
5楼-- · 2018-12-31 08:29

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);

    }

}
查看更多
高级女魔头
6楼-- · 2018-12-31 08:30

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);
查看更多
初与友歌
7楼-- · 2018-12-31 08:32
import java.util.Scanner;

public class userInput{
    public static void main(String[] args){
        // Creating your scanner with name kb as for keyBoard
        Scanner kb = new Scanner(System.in);

        String name;
        int age;
        char bloodGroup;
        float height;

        // Accepting Inputs from user
        System.out.println("Enter Your Name");
        name = kb.nextLine(); // for entire line of String including spaces
        System.out.println("Enter Your Age");
        age = kb.nextInt(); // for taking Int
        System.out.println("Enter Your BloodGroup : A/B/O only");
        bloodGroup  = kb.next().charAt(0); // For character at position 0
        System.out.println("Enter Your Height in Meters");
        height = kb.nextFloat(); // for taking Float value

        // closing your scanner object
        kb.close();

        // Outputting All
        System.out.println("Name : " +name);
        System.out.println("Age : " +age);
        System.out.println("BloodGroup : " +bloodGroup);
        System.out.println("Height : " +height+" m");

    }
}
查看更多
登录 后发表回答