如何通过 SpringSource Eclipse 在 Groovy 中使用 readLine 读取多行?
我试图从 Spring Source Suite 2.7.1 中的 Groovy 1.7.10 中的控制台读取多个输入(多个 in.readLine()),但控制台似乎只允许我输入第一个条目,为第二个条目,然后移动让我输入第三个条目,为第四个条目添加空白,等等。以下是我正在使用的代码。
System.in.withReader {reader ->
print "w: "
def w = reader.readLine()
print "x: "
def x = reader.readLine()
print "y: "
def y = reader.readLine()
print "z: "
def z = reader.readLine()
}
我得到的输出如下。
宽:1 x:y:2 z:
如您所见,它让我输入第一行,为第二行留一个空白,让我输入第三个输入,并为第四行留一个空白。这与我输入输入时按“enter”有关吗?我认为不会,因为 readLine() 应该读取一行并检索文本,但不包括回车符。任何帮助表示赞赏。
谢谢,
胡安
I am trying to read multiple inputs from the console in Groovy 1.7.10 in Spring Source Suite 2.7.1 (multiple in.readLine()), but the console only seems to allow me to input the first entry, puts in a blank for the second entry and moves to let me enter the third entry, puts in a blank for the fourth entry etc. The following is the code that I'm using.
System.in.withReader {reader ->
print "w: "
def w = reader.readLine()
print "x: "
def x = reader.readLine()
print "y: "
def y = reader.readLine()
print "z: "
def z = reader.readLine()
}
The output that I am getting is as follows.
w: 1
x: y: 2
z:
As you can see, it lets me enter the first line, puts a blank for the second, and lets me enter the third input, and puts a blank for the fourth. Does this have to do with me pressing "enter" when entering my input? I would think not since readLine() is supposed to read a line and retrieve the text upto but not including the carriage returns. Any help is appreciated.
Thanks,
Juan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是 Java 6+,您可以尝试使用
Console
类来读取这些行吗?不确定
Console
是否在 STS 内工作...Assuming you are on Java 6+, can you try using the
Console
class to read the lines?Not sure if
Console
works inside STS though...我猜这是在 Windows 上......似乎有一个方法
readLine(booleanignoreLF)
,但它不可见,或者不起作用。我也遇到了这个问题。目前,只需每次额外调用
reader.readLine()
即可。I'm guessing this is on Windows ... there appears to be a method
readLine(boolean ignoreLF)
, but it is not visible, or otherwise doesn't work. I'm running into this issue as well.For now, just run an extra call to
reader.readLine()
each time.