Reputation: 5622
I started learning groovy, and since I like learning by practice I have written that small app:
def height = 1; def commands; def floors; def finished = false
def up = { height < 5 ? height++ : println("Can't go higher!") }
def down = { height > -1 ? height-- : println("Can't go lower!") }
def help = {
print "Commands are "
commands.each{key, val -> print "'$key' "}
println()
}
def printFloors = {
println "Floors are "
floors.each{key,val -> println "'$key' -> '$val'"}
println()
}
def exit = { finished = true }
def prompt = { print floors["$height"] }
commands = [ 'u': up,
'up': up,
'd': down,
'down': down,
'help': help,
'': help,
'exit': exit,
'pf': printFloors]
floors = [ "-1": "Basement : " ,
"0": "Ground : " ,
"5": "Penthouse : " ]
(1..4).each{floors += ["${it}" : "Floor ${it} : " ] }
bR = new BufferedReader(new InputStreamReader(System.in))
while(!finished){
prompt()
def cmd = bR.readLine()
def code = commands[cmd]
if(code != null){
code()
}
}
Everything works fine except printing on which floor you are (prompt function). It prints if you are in basement, ground floor or penthouse, but doesn't pick up "Floor i: " and prints null instead:/ When I type "pf" to print my floors dictionary, values are there... Any ideas? Thanks
Upvotes: 0
Views: 304
Reputation: 171104
You're adding GString
instances as keys in your map, then searching for them using String
instances.
The two are not the same (despite having the same appearance -- see "GStrings aren't Strings" section on this page)
Try changing:
(1..4).each{floors += ["${it}" : "Floor ${it} : " ] }
to
(1..4).each{floors += [ ("${it}".toString()): "Floor ${it} : " ] }
Upvotes: 7