Java 获取字符的扫描仪方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2597841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:41:44  来源:igfitidea点击:

Scanner method to get a char

javacharjava.util.scanner

提问by bizarrechaos

What is the Scannermethod to get a charreturned by the keyboard in Java.

在Java中Scanner获取char键盘返回的方法是什么。

like nextLine()for String, nextInt()for int, etc.

比如nextLine()for StringnextInt()forint等。

采纳答案by polygenelubricants

To get a charfrom a Scanner, you can use the findInLinemethod.

要从 achar获取 a Scanner,您可以使用该findInLine方法。

    Scanner sc = new Scanner("abc");
    char ch = sc.findInLine(".").charAt(0);
    System.out.println(ch); // prints "a"
    System.out.println(sc.next()); // prints "bc"

If you need a bunch of charfrom a Scanner, then it may be more convenient to (perhaps temporarily) change the delimiter to the empty string. This will make next()returns a length-1 string every time.

如果您需要一堆charfrom a Scanner,那么(可能是暂时的)将分隔符更改为空字符串可能更方便。这将使next()每次返回一个长度为 1 的字符串。

    Scanner sc = new Scanner("abc");
    sc.useDelimiter("");
    while (sc.hasNext()) {
        System.out.println(sc.next());
    } // prints "a", "b", "c"

回答by Itay Maman

You can use the Console API(which made its appearance in Java 6) as follows:

您可以使用控制台 API(它在 Java 6 中出现),如下所示:

Console cons = System.console();
if(cons != null) {
  char c = (char) cons.reader().read();  // Checking for EOF omitted
  ...
}

If you just need a single line you don't even need to go through the reader object:

如果你只需要一行,你甚至不需要通过 reader 对象:

String s = cons.readLine();

回答by eipxen

Java's Scanner class does not have a built in method to read from a Scanner character-by-character.

Java 的 Scanner 类没有内置的方法来逐个字符地从 Scanner 中读取。

http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

However, it should still be possible to fetch individual characters from the Scanner as follows:

但是,仍然可以从 Scanner 获取单个字符,如下所示:

Scanner sc:

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

And you could use it to fetch each character in your scanner like this:

您可以使用它来获取扫描仪中的每个字符,如下所示:

while(sc.hasNext()){
    char c = sc.findInLine(".").charAt(0);
    System.out.println(c); //to print out every char in the scanner
}

The findInLine() method searches through your scanner and returns the first String that matches the regular expression you give it.

findInLine() 方法搜索您的扫描仪并返回与您提供的正则表达式匹配的第一个字符串。

回答by Satish Singhal

Console cons = System.console();

The above code line creates cons as a null reference. The code and output are given below:

上面的代码行将 cons 创建为空引用。代码和输出如下:

Console cons = System.console();
if (cons != null) {
    System.out.println("Enter single character: ");
    char c = (char) cons.reader().read();
    System.out.println(c);
}else{
    System.out.println(cons);
}

Output :

输出 :

null

空值

The code was tested on macbook pro with java version "1.6.0_37"

代码在 macbook pro 上测试,java 版本为“1.6.0_37”

回答by user3178887

sc.next().charat(0).........is the method of entering character by user based on the number entered at the run time

sc.next().charat(0).........是用户根据运行时输入的数字输入字符的方法

example: sc.next().charat(2)------------>>>>>>>>

示例:sc.next().charat(2)------------>>>>>>>>

回答by Teena Sachdeva

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