Julian20151006
Julian20151006

Reputation: 321

How to use System.in.readLine() in Groovy?

I wrote a groovy script like this:

print "Please enter your name:"
def name=System.in.readLine()
println "My name is : ${name}"

But when I ran it ,I got an exception:

Exception thrown groovy.lang.MissingMethodException: No signature of method: java.io.BufferedInputStream.readLine() is applicable for argument types: () values: [] Possible solutions: readLines(), readLines(java.lang.String), eachLine(groovy.lang.Closure), eachLine(java.lang.String, groovy.lang.Closure), eachLine(int, groovy.lang.Closure), eachLine(java.lang.String, int, groovy.lang.Closure)

And I found System.in.readLines() did work, but that method read multiple lines.
Besides, the basic input function can only work in command line. In GroovyConsole,when I run the script, I can't input anything.
Any veteran can help me? Thanks a lot!

Upvotes: 1

Views: 4854

Answers (1)

rvargas
rvargas

Reputation: 635

Use System.console().readLine()

def name=System.console().readLine("Please enter your name: ")
println "My name is : ${name}"

Upvotes: 1

Related Questions