0

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 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

not "equals", in groovy this is very badly implemented and will return false in between String and GString, try to use ==
you can refer to this topic in that stackoverflow.com/questions/9682206/…
i am not sure why but still this is not working if("${env}" == "syst")
ohh yeah, its working, i had something else failing , '==' is perfectly working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.