Groovy: Groovy Console: Solution for Input from User Bug

I am currently (Dec 13, 2016) taking a class on Groovy, and I found that the Groovy console have a huge bug which doesn’t allow it to obtain input from the user. System.console() gives ‘null’ and all the solutions involving the use of System.in or the input reader buffer seems to fail.

These input-from-user bugs seems to be known based on what the instructor told me when I showed this solution. After some research, I found that they were reported but nothing have being done so far to solve the issue.

Below is the only solution I found so far that works, creating a dialog in order to obtain the input from the user and execute a code.
Please notice that if you close the dialog (pressing the X button), you will kill the groovy console too.

import groovy.swing.SwingBuilder;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
def sb = new SwingBuilder()
sb.frame(title: 'Action',
         location: [300, 55],
         pack: true,
         show: true,
         defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {

    gridLayout(columns: 2, rows: 4)
    label('Continue [ Y/N ]:')
    textField(id: 'input')       
    
    button(text: 'Execute', actionPerformed: {
       execute(input.text, sb)
    })            
}

def execute(input, sb){    
    // Your code
    output("User andwer: ${input}", sb) 
    // More of your code
}

def output(output, sb){
    def out = (output) ? output : ""
    sb.optionPane().showMessageDialog(null, 
                                      "$output", 
                                      "Executing",
                                      JOptionPane.INFORMATION_MESSAGE)
}
Share
Leave a comment

Groovy Tutorial – Part 1

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

Note: If you are not interested to know about the differences between Groovy and Java, skip to part 2.

Groovy is a dynamic language (which means many behaviors are executed at runtime instead of during compilation) based on the Java Virtual machine. This means that Groovy is tightly integrated with the Java language.

The biggest differences between Groove and Java are:

  1. The following packages and classes are imported by default (no need to use an explicit import to use them):
    • java.io.*
    • java.lang.*
    • java.math.BigDecimal
    • java.math.BigInteger
    • java.net.*
    • java.util.*
    • groovy.lang.*
    • groovy.util.*
  2. These are optionals:
    • Semicolons ‘;’ are optional
    • The keyword ‘return’ is optional
  3. The keyword ‘this’ can be used inside static methods.
  4. Since in Groovy there is no difference between checked and unchecked exceptions, the clause ‘Throw’ in a method signature is not checked
  5. There is no compile errors for using:
    • Undefined members
    • Passing Arguments of the wrong type
  6. The == operator and the method is()
    • The operator == will check for equality (checking if both object are equal) instead for identity (checking if both variables point the same object)
    • The method is() allows for checking for identity.
      • Example: foo.is(bar)
      • This doesn’t work on null. For checking null, you can still using == operator
  7. New features added to Groovy not available in Java:
    1. Closures: piece of code which is defined and then executed at a later; also known as “code block” or method pointer.
    2. Lists and maps  are native syntax which means that Groovy threads them as first class construct.
    3. Groovy provide native support for regular expressions
    4. Dynamic and static typing is supported
    5. Expressions can be embedded inside strings
    6. GPars (Groovy Parallel Systems) framework support for parallel processing. It supports:
      • Actors
      • Map and Reduce
      • Fork and Join
      • Dataflow
    7. GPath (Path Expression Language): Allows parts of nested structured data to be identified.
    8. GroovyMarkup: Native support for markup languages such as XML, HTML, W3C DOM, Ant Tasks, SAX, Swing, etc.
    9. Polymorphic interaction: Similar to STL standard iterators with these following differences:
      • Declarations wrapped in traversable template which makes the collection accessible
      • Constant iterator declaration is not scoped by the collection class. Instread the collection is passed in as a constructor argument.
    10. Switch statement:
      • In java, we can only use the int or int-derived type classifier.
      • In Groovy, we can any classifiers for a switch statement.
    11. Static and dynamic typing are supported.
      • If you wish to use a dynamic types variable you can use the keyword ‘def’
    12. Writing beans with simpler syntax for both properties and adding even listeners.
    13. Simplified way to check null which eliminate nasty nested ifs
    14. All variables are reference to objects. Groove doesn’t use primitive variables.
      • Even do you can write a primitive type as a short form, Groove will always translate it into the object.

Part 2 comming up soon…

Share
Leave a comment