开发者

How do you read multiple lines using readLine in Groovy through SpringSource Eclipse?

开发者 https://www.devze.com 2023-04-08 23:33 出处:网络
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

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.read开发者_如何学编程Line()
        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


Assuming you are on Java 6+, can you try using the Console class to read the lines?

def values = System.console().with { 
  [ 'w', 'x', 'y', 'z' ].inject( [:] ) { map, val ->
    map << [ (val):readLine( "$val: " ) ]
  }
}

println "Got w=$values.w x=$values.x y=$values.y z=$values.z"

Not sure if Console works inside STS though...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号