24

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

James
  • 307
  • 1
  • 3
  • 12
  • When does this error occur? How did you start `groovyConsole`? – calebds Apr 17 '12 at 02:09
  • This error occurs when I run the script from GroovyConsole. I just started GroovyConsole with the 'Start GroovyConsole' shortcut. – James Apr 17 '12 at 02:16
  • I can't reproduce. Try running from terminal, e.g. `> groovyConsole` then standard input is received through the terminal. This might be of use to you http://groovy.329449.n5.nabble.com/Input-from-GroovyConsole-td342416.html. – calebds Apr 17 '12 at 02:24
  • 1
    Thanks for the link. Though I am still a bit confused on how to run a script from the terminal? How do I access it? – James Apr 17 '12 at 03:05

5 Answers5

44

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:

def username = System.console().readLine 'What is your name?'
println "Hello $username"

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:

println "What is your name?"
println "Your name is ${System.in.newReader().readLine()}"
Renato
  • 12,940
  • 3
  • 54
  • 85
25
def readln = javax.swing.JOptionPane.&showInputDialog
def username = readln 'What is your name?'
println "Hello $username."
user1845761
  • 421
  • 5
  • 5
  • 2
    This is very cool and succint, how do you get the "ok/cancel" value? Nevemind, got it.. it returns null if you hit cancel. Thanks! – Bill K Jan 25 '13 at 23:11
  • Nice one. Could also be `import static javax.swing.JOptionPane.showInputDialog` and `def username = showInputDialog 'What is your name?'` – Jeremy Hunt Oct 16 '19 at 00:12
12

Your code works.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
print "Input:"
def userInput = br.readLine()
println "You entered: $userInput"

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. enter image description here

Larry Battle
  • 9,008
  • 4
  • 41
  • 55
3

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]:

def cons = System.console()
def yn
if (cons) {
    yn = {((cons.readLine(it + " (y/n) ")?:"n").trim()?:"n")?.charAt(0).toLowerCase().toString() }
} else {
    cons = javax.swing.JOptionPane.&showInputDialog
    yn = {((cons(it + " (y/n) ")?:"n").trim()?:"n")?.charAt(0).toLowerCase().toString() }
}
if (yn("Did you want to do something?") == 'y')
    ...do something here!...
Merlin
  • 31
  • 1
2

if your System.console() is null, you can

Scanner scan = new Scanner(System.in);
String s = scan.nextLine()
689
  • 529
  • 5
  • 6