1

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!

Julian20151006
  • 191
  • 6
  • 12
  • notice the proposed solution "System.console().readLine()" no longer works, for another solution, see https://stackoverflow.com/questions/47348142/console-readline-throws-nullpointer-exception-in-groovy – Fernando Gonzalez Sanchez Mar 07 '22 at 09:39

1 Answers1

1

Use System.console().readLine()

def name=System.console().readLine("Please enter your name: ")
println "My name is : ${name}"
rvargas
  • 635
  • 6
  • 13
  • Yes! It works! Thank you a lot! But I'm still curious. Why doesn't System.in.readLine() work? The instruction book did it that way – Julian20151006 Oct 30 '16 at 03:34