如何从控制台读取参数到groovy中的方法?
问题描述:
我是groovy.I新手,正在从控制台读取两行代码的值。如何从控制台读取参数到groovy中的方法?
System.in.withReader {
println "Version: "
version = it.readLine()
println "Doc Type:"
Doc=it.readLine()
call getBillID(version,Doc)
}
getBillid
方法如下,
def getBillID(int version,int doc)
{
allNodes.BillID.each {
theregularExpression=/\d+_\d+_\d+_\d_\d+_\d+_\d_${version}_${Doc}_\d+_\d+/
if(it != "" && it =~ theregularExpression) {
println "******" + it
}
}
}
现在我想在我的getBILLID
方法使用这些变量的值,但我得到的错误作为
No signature of method: ReadXML.getBillID() is applicable for argument types: (java.lang.String, java.lang.String) values: [9, ]
我哪里错了。可以任何一个告诉我plz ..
答
除了@ Kalarani的答案,你也可以这样做:
System.in.withReader {
print "Version: "
int version = it.readLine() as int
print "Doc Type: "
int doc = it.readLine() as int
getBillID(version, doc)
}
顺便说一句;我会小心你的大小写和变量名称,即:你有一个变量,名字为Doc
,大写字母。这不是标准的命名方案,您最好使用全部小写字母来表示变量名称。你可以看到它在getBillID
方法中的困惑。该参数被称为doc
(全部小写),但在正则表达式中,您引用${Doc}
(再次大写)。
这样的事情将要最终导致你的疼痛和错误的世界,可能需要更长的时间来找到
什么类型allnodes中?看起来系统期望从XML文件读取,allNodes看起来像是面向XML的,这使得很难看到你想从控制台获取值。 – OldTroll 2011-05-27 18:53:41