I'm just starting to learn Groovy and I am experimenting in GroovyConsole.
Is there a way I can read user input? I have tried the code below but I get an error.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) print "Input:" input = br.readLine() println "You entered: $input"
This is the error I am receiving:
Exception thrown 17-Apr-2012 02:52:39 org.codehaus.groovy.runtime.StackTraceUtils sanitize WARNING: Sanitizing stacktrace: java.io.IOException: Stream closed
Is there anything I need to import?
Any help would be great.
Thanks
I got here trying to find out the easiest way to read user input from the command line... I found the answer elsewhere, will post here to document the 'real' Groovy way as it's still missing:
As Larry Battle says, if using the groovy console, make sure to look at the background 'black' window for the output and to type input.
EDIT
In an environment where Console is not available, such as running from your IDE, probably, use this instead:
Your code works.
Assuming you're on windows, the only problem is that the input is being read from the console in the background that is launched before groovyconsole.
if your System.console() is null, you can
You could try something like this, which works at the command-line of any o/s, but also in the GoovyConsole - where it pops up a dialog [as noted in a previous post]: