Not able to compare string entered from Input Dialog with a different string in Groovy

86 views Asked by At

Below is my code:

def readln = javax.swing.JOptionsPane.&showInputDialog
def env = readln 'Which environment you want to test'

I entered input as syst

While i am comparing this is what i am doing

if("$env".equalsIgnoreCase("syst")){
some code
}

also tried many other ways to compare like

if($env.equalsIgnoreCase("syst"))
if(env.equalsUIgnoreCase("syst"))
if("${'env'}".equalsIgnoreCase("syst"))

but none of the above working , the condition is not satisfied. How to compare the string declared with a string entered from dialog box?

2

There are 2 answers

4
lGSMl On

try expand it directly to a string as - "${env}"

0
daggett On

first - the classname JOptionsPane is wrong (it's JOptionPane - without the s)

below is the working code.

you can run it from groovy console.

import javax.swing.JOptionPane

def readln = JOptionPane.&showInputDialog
def env = readln 'Which environment you want to test'
if(env=='syst'){
    println "EQUALS"
}
if('syst'.equalsIgnoreCase(env)){
    println "EQUALS equalsIgnoreCase 1"
}
if(env.equalsIgnoreCase('syst')){
    println "EQUALS equalsIgnoreCase 2"
}
if("${env}".equalsIgnoreCase('syst')){
    println "EQUALS equalsIgnoreCase 3"
}

all 4 comparisons works fine.

however 'syst'.equalsIgnoreCase(env) is preferable if you'd like to compare ignoring case.

because the env could be null at this point