扫描仪方法获取字符
Java中获取键盘返回的char
的Scanner
方法是什么?
例如 nextLine()
代表 String
,nextInt()
代表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要从
Scanner
获取char
,您可以使用findInLine
方法。如果您需要来自
Scanner
的一堆char
,那么将分隔符(也许暂时)更改为空字符串可能会更方便。这将使next()
每次都返回一个长度为 1 的字符串。To get a
char
from aScanner
, you can use thefindInLine
method.If you need a bunch of
char
from aScanner
, then it may be more convenient to (perhaps temporarily) change the delimiter to the empty string. This will makenext()
returns a length-1 string every time.您可以使用 Console API(在 Java 6 中出现)如下:
如果您只需要一行,您甚至不需要通过 reader 对象:
You can use the Console API (which made its appearance in Java 6) as follows:
If you just need a single line you don't even need to go through the reader object:
Java 的 Scanner 类没有内置方法来从 Scanner 中逐个字符读取。
http://java.sun.com/javase/ 6/docs/api/java/util/Scanner.html
但是,仍然可以从扫描仪中获取单个字符,如下所示:
并且您可以使用它来获取扫描仪中的每个字符,如下所示
: 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:
And you could use it to fetch each character in your scanner like this:
The findInLine() method searches through your scanner and returns the first String that matches the regular expression you give it.
上面的代码行将 cons 创建为空引用。代码和输出如下:
输出:
代码在 macbook pro 上测试,java 版本为“1.6.0_37”
The above code line creates cons as a null reference. The code and output are given below:
Output :
The code was tested on macbook pro with java version "1.6.0_37"
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)------------>>>>>>>>