title: Unexpected token: while at line:
body: <p>I am new to Groovy and don't have hands-on experience in Java. Could anyone answer my naive question in the below code.</p>
  
  <pre><code>class StudentMap { 
  
  def student_map=new HashMap&lt;String,String&gt;()
  
  def answer=System.console().readLine 'Do you want to add a student Y/N\n'  
  
  while (answer=='Y') { 
      get_student()
      answer=System.console().readLine 'Do you want to continue Y/N\n' 
      }
  
  def add_student(String name,String age)
       {  
          student_map &lt;&lt; ["${name}":"${age}"]  
       }   
  
  def get_student()
      {  
          def name=System.console().readLine 'What is your name\n'
          def age=System.console().readLine 'What is your age\n'
          add_student(name,age) 
      }
   print student_map
  } 
  def Student_Map = new StudentMap() 
  </code></pre>
  
  <p>I am getting the below error</p>
  
  <blockquote>
    <p>1 compilation error:</p>
    
    <p>unexpected token: while at line: 7, column: 5</p>
  </blockquote>
  
  <p>I checked the brackets but they seem fine. Could anyone tell me whats the error in this code.
  I just want to create a HashMap each time and store students information like age and name
  Basically when ever i run this i would want to create a HashMap of students information and print it.</p>
  
  <p>Also could anyone suggest any good sites offering Groovy Examples from Beginner to Expert as most of the results that i found online are more java programmer friendly.</p>
  
  <p>Thanks in advance</p>
  
  <p>*****This Answer Contains 2 Updates*****</p>
  
  <p>Update 1</p>
  
  <p>Thanks for the reply Nathan.. Below is the code i had initially written to achieve the following :: 1.Create a Hashmap of students everytime the code is run 2.Ask the user to enter Student information 3.When no more students are to be added display the HashMap</p>
  
  <p>My code WITHOUT using class is</p>
  
  <pre><code>answer=System.console().readLine 'Do you want to add a student Y/N\n'
  
  while (answer=='Y') { 
  get_student()
  answer=System.console().readLine 'Do you want to continue Y/N\n' 
  }
  
  def add_student(String name,String age)
   {  
  
      def student_map=new HashMap&lt;String,String&gt;()
      student_map &lt;&lt; ["${name}":"${age}"]  
   }   
  
  def get_student()
  {  
      def name=System.console().readLine 'What is your name\n'
      def age=System.console().readLine 'What is your age\n'
      add_student(name,age) 
  }
  </code></pre>
  
  <p>Apart from displaying, which should not be a big problem this code runs fine but i guess it creates a HashMap for each student entry which i do not want. And if i declare the def student_map=new HashMap() outside the add_student function the function cannot reference it. Thats the reason i thought of making a Student class so everytime i make a object of the class student_map through def Student_Map = new StudentMap(), it would create a Student_map object whose property would be the HashMap student_map which can have two actions</p>
  
  <p>1.Get student information from user through get_student() function. 2.Add the student information into the student_map HashMap using add_student(String name,String age) function.</p>
  
  <p>When no more addition is required display the contents of the HashMap student_map.</p>
  
  <p>Please let me know if my understanding and implementation is correct and efficient.</p>
  
  <p>Update 2 on the Same:</p>
  
  <p>I tried to run the code as suggested by putting the while and print inside a method main() which compiled fine but is still not logically correct it seems as whenever i run the code it executes and does not ask for user input.</p>
  
  <p>Below is the updated code i am trying to run to achieve what i mentioned in the above update of this post.</p>
  
  <pre><code>class StudentMap {
  
  void main(String[] args) {
  
  answer=System.console().readLine 'Do you want to add a student Y/N\n'  
  
  def student_map=new HashMap&lt;String,String&gt;()
  
  while (answer=='Y') { 
      get_student()
      answer=System.console().readLine 'Do you want to continueY/N\n' 
   }
  
  }
  def add_student(String name,String age) {
  student_map &lt;&lt; ["${name}":"${age}"]
  }
  
  def get_student()
  {  
      def name=System.console().readLine 'What is your name\n'
      def age=System.console().readLine 'What is your age\n'
      add_student(name,age) 
  }
  } def Student_Map = new StudentMap()
  </code></pre>
  
  <p>Result: StudentMap@7e54423</p>
  
  <p>I removed the static in the void main function as it was giving me the below error.</p>
  
  <p>Apparent variable 'answer' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'answer' but left out brackets in a place not allowed by the grammar.</p>
  
  <p>Without the static keyword my code compiles fine. I suppose my object is getting created but is not behaving the way i want it to.</p>
  
  <p>Could anyone please let me know where i am going wrong.</p>