扫描仪方法获取字符

发布于 2024-08-27 22:17:35 字数 167 浏览 4 评论 0原文

Java中获取键盘返回的charScanner方法是什么?

例如 nextLine() 代表 StringnextInt() 代表 int 等。

What is the Scanner method to get a char returned by the keyboard in Java.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雄赳赳气昂昂 2024-09-03 22:17:35

要从 Scanner 获取 char,您可以使用 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"

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

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

To get a char from a Scanner, you can use the findInLine method.

    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 char from 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.

    Scanner sc = new Scanner("abc");
    sc.useDelimiter("");
    while (sc.hasNext()) {
        System.out.println(sc.next());
    } // prints "a", "b", "c"
何以心动 2024-09-03 22:17:35

您可以使用 Console API(在 Java 6 中出现)如下:

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

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

String s = cons.readLine();

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

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:

String s = cons.readLine();
公布 2024-09-03 22:17:35

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

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

但是,仍然可以从扫描仪中获取单个字符,如下所示:

Scanner sc:

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

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

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

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

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

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 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.

惯饮孤独 2024-09-03 22:17:35
Console cons = System.console();

上面的代码行将 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);
}

输出:

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

Console cons = System.console();

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

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"

假面具 2024-09-03 22:17:35

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

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

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

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

云柯 2024-09-03 22:17:35
Scanner sc = new Scanner (System.in)
char c = sc.next().trim().charAt(0);
Scanner sc = new Scanner (System.in)
char c = sc.next().trim().charAt(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文