Hai*_*ini 5 java java.util.scanner
我正在尝试通过ScannerObject 读取命令.检查我使用的输入语法sc.hasNext()(对于缺少命令的情况).它已经很好地适用于许多情况,但现在我已经在JavaAPI中描述为"可以阻塞并等待输入"的情况.
该hasNext()方法何时阻止,我该如何控制它?有趣的事情是它在块之前的3个案例中完美地工作.此外,JavaAPI描述hasNext()了检查是否存在另一个Input的正确方法,以便Method next()不生成Exception.
这是我到目前为止所做的代码:
if (sc.hasNext() && sc.next().equals("create")) {
if (sc.hasNextInt()) {
width = sc.nextInt();
if (width > 0) {
if (sc.hasNextInt()) {
heigth = sc.nextInt();
if (heigth > 0) {
if (sc.hasNext()) { //At this point the hasNext Statement blocks for //no reason till an Input is made.
charset = sc.next();
Image = new AsciiImage(width, heigth,charset);
} else {
ret = false;
System.out.println("INPUT MISMATCH");
}
} ...//and so on
Run Code Online (Sandbox Code Playgroud)
在此先感谢,我在这个主题上找不到任何东西.编辑:扫描程序被定义为System.in,但这不应该是一个问题 - 至少它至今不是一个.
通过Console或TextFile进行测试之间存在差异.如果我从Console读取,程序需要一个Stream,它将等待进一步的输入.
当通过Textfile中的Input进行测试时(仍然使用System.in进行扫描程序,使用Java程序),hasNext()将在文件末尾返回false,因为无法进一步输入.
我无法在这个主题上找到文档(在https://docs.oracle.com/javase/9/docs/api/java/util/Scanner.html#hasNext--中).因此,如果有人找到一个正确和技术正确的答案,我会非常非常好.
| 归档时间: |
|
| 查看次数: |
9247 次 |
| 最近记录: |