0

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

jcb
  • 195
  • 1
  • 4
  • 20

2 Answers2

0

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.

Kevin McCarpenter
  • 786
  • 1
  • 7
  • 22
0

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...

tim_yates
  • 167,322
  • 27
  • 342
  • 338