Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:38:30+00:00 2026-05-31T08:38:30+00:00

I started learning groovy, and since I like learning by practice I have written

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:38:31+00:00Added an answer on May 31, 2026 at 8:38 am

    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} : " ] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning Jquery and am new to writing javascript (I am
Just started learning Groovy, got the PragProg book Programming Groovy and had a problem
I started learning LISP and I heard that there is some kind of a
I started learning jQuery just yesterday, and I like it very much. And now
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I started learning REGEX 2 days ago , now id like to make a
Just started learning Haskell. I have an empty source file with this inside: pe
Hi I have just started learning about xsd as I want to use it
I started learning how to use Selenium today. I have never used it before.
I started learning Python using the IronPython implementation. I'd like to do some web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.