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?

Ram
  • 59
  • 1
  • 10

2 Answers2

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

cfrick
  • 35,203
  • 6
  • 56
  • 68
daggett
  • 26,404
  • 3
  • 40
  • 56
0

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

lGSMl
  • 151
  • 1
  • 11
  • not "equals", in groovy this is very badly implemented and will return false in between String and GString, try to use == – lGSMl Dec 23 '18 at 22:00
  • you can refer to this topic in that https://stackoverflow.com/questions/9682206/groovy-different-results-on-using-equals-and-on-a-gstringimpl – lGSMl Dec 23 '18 at 22:02
  • i am not sure why but still this is not working `if("${env}" == "syst")` – Ram Dec 23 '18 at 23:03
  • ohh yeah, its working, i had something else failing , '==' is perfectly working. – Ram Dec 24 '18 at 01:34