0

I have been working on something with ANTLRv4 this morning, and I have a need to take a certain string from the user, defined in the grammar as:

OP : 'a' | 'b' | 'c';

In a {} Java-thingy after a different definition, I need to compare the OP to another string. Currently, this is not working:

if ($string.text == "a") {
    //Do Something
}

where string is the name I attached to that specific instance of OP. How can I compare the text value of string to another string ("a")?

2 Answers2

1

You probably need to do:

$string.text.equals("a")

mjs
  • 21,431
  • 31
  • 118
  • 200
1

The '==' operator will compare references in Java. To compare the value of the strings you have to use the equals() method.

Try this:

if ($string.text.equals("a")) {
    //Do Something
}

Read this for more info on the operators: What's the difference between ".equals" and "=="?

Community
  • 1
  • 1
Zarwan
  • 5,537
  • 4
  • 30
  • 48