扫描器方法获取一个char [英] Scanner method to get a char

查看:618
本文介绍了扫描器方法获取一个char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scanner 方法用于获取Java中键盘返回的 char

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

nextLine() for String nextInt() int 等。

推荐答案

要从扫描仪获取 char ,您可以使用 findInLine 方法。

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"



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

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"

这篇关于扫描器方法获取一个char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆