question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to add Strings as operators (e. g. AND, OR)?

See original GitHub issue

Is it possible to allow a custom operator to be a string? Example: replace && with AND.

I tried it with this:

Expression e = new Expression("if( a == 1 AND b == 1, 1, 0)");

e.addOperator(new AbstractOperator("AND", Expression.OPERATOR_PRECEDENCE_AND, false, true) {
  @Override
  public BigDecimal eval(BigDecimal v1, BigDecimal v2) {
	
	boolean b1 = v1.compareTo(BigDecimal.ZERO) != 0;
	
	if (!b1) {
	  return BigDecimal.ZERO;
	}
	
	boolean b2 = v2.compareTo(BigDecimal.ZERO) != 0;
	return b2 ? BigDecimal.ONE : BigDecimal.ZERO;
  }
});

BigDecimal result = e.with( "a", "0").and( "b","1").eval(); 

System.out.println(result);

But that results in the exception: Function if expected 3 parameters, got 5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
uklimaschewskicommented, Sep 8, 2019

@Roland09 I have created version 2.4 with your proposed fix. Feel free to open a pull request directly next time. 😃

0reactions
Roland09commented, Sep 6, 2019

@uklimaschewski I guess the solution to the mentioned bug is to simply switch the if-clause, i. e. instead of this:

https://github.com/uklimaschewski/EvalEx/blob/88456407b2043571ae0744d1712c19fa7fbb954f/src/main/java/com/udojava/evalex/Expression.java#L457-L463

it should be this

if (operators.containsKey(token.surface)) {
	token.type = TokenType.OPERATOR;
} else if (ch == '(') {
	token.type = TokenType.FUNCTION;
} else {
	token.type = TokenType.VARIABLE;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Concatenation Operators in Visual Basic | Microsoft Learn
The & Operator is defined only for String operands, and it always widens its operands to String , regardless of the setting of...
Read more >
Use string operators and wildcards - Apple Support
The string operator (&) can be used in formulas to concatenate, or join, two or more strings or the contents of referenced cells....
Read more >
How does adding String with Integer work in JavaScript?
If one or both operands is string, then the plus is considered as string concatenation operator rather than adding numbers. the minus operator...
Read more >
String Addition Operator | Arduino Documentation
operator allows you to combine a String with another String, with a constant character array, an ASCII representation of a constant or variable ......
Read more >
How to add Strings as Numbers in JavaScript | bobbyhadz
Use the addition (+) operator, e.g. Number('1') + Number('2') . The addition operator will return the sum of the numbers. index.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found