为什么hasNextLine()永远不会结束? [英] Why does hasNextLine() never end?

查看:379
本文介绍了为什么hasNextLine()永远不会结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这听起来太简单了。我是Java的新手。

Sorry if this sounds too simple. I'm very new to Java.

这是我用来检查 hasNextLine()的一些简单代码。当我运行它时,我无法阻止它。我想如果你没有写任何输入并按下 Enter ,你将在循环时转义

Here is some simple code I was using to examine hasNextLine(). When I run it, I can't make it stop. I thought if you didn't write any input and pressed Enter, you would escape the while loop.

有人可以向我解释在这种情况下 hasNextLine()是如何工作的?

Can someone explain to me how hasNextLine() works in this situation?

import java.util.*;

public class StringRaw {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
        }
        System.out.print("YOU'VE GOT THROUGH");
    }
}


推荐答案

从System.in读取,默认情况下,您正在从键盘读取,这是一个无限输入流...它具有与用户键入的一样多的行。我认为发送EOF的控制序列可能有效,例如CTL-Z(或CTL-D?)。

When reading from System.in, you are reading from the keyboard, by default, and that is an infinite input stream... it has as many lines as the user cares to type. I think sending the control sequence for EOF might work, such as CTL-Z (or is it CTL-D?).

查看我的好橙色图表...... CTL-C是ETX,CTL-D是EOT;其中任何一个都应该用于终止文本流。 CTL-Z是一个不应该工作的SUB(但它可能,因为控件在历史上是高度主观地解释的)。

Looking at my good-ol' ASCII chart... CTL-C is an ETX and CTL-D is an EOT; either of those should work to terminate a text stream. CTL-Z is a SUB which should not work (but it might, since controls are historically interpreted highly subjectively).

这篇关于为什么hasNextLine()永远不会结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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