java hasnext_java – hasNext() – 什么时候阻止,为什么?

我正在尝试通过扫描仪对象读取命令.为了检查输入语法,我使用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

在此先感谢,我在这个主题上找不到任何东西.

编辑:扫描程序被定义为System.in,但这不应该是一个问题 – 至少它至今不是一个.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值