-2

How can I use a character in Java as an expression, like suppose I have an expression

char ch = '+';

so is it possible to perform addition using value of ch, without using if-else or switch or any thing like that?

I want to use ch as expression. if ch = '+', then is it possible to do something like x = 5 ch 6 to calculate 5+6?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Unaware Cub
  • 37
  • 1
  • 3
  • I don't understand your question. Are you asking if you can define custom operators in Java? – Duncan Jones Mar 18 '15 at 12:25
  • No, let me clarify. I want to use ch as expression. if ch = '+', then is it possible to do something like x = 5 _ch_ 6 to calculate 5+6 – Unaware Cub Mar 18 '15 at 12:35
  • I think he's asking if he can define a char `ch` as `+` then execute some arithmetic like `1 ch 2` would equal `3`. – mmmmmpie Mar 18 '15 at 12:36
  • 1
    @EdwardKenway OK, that sounds exactly like defining a custom operator. Maybe you should explain *why* you want to do this, it may help us suggest a better answer. Note: please [edit] your question to include this information. – Duncan Jones Mar 18 '15 at 12:37

4 Answers4

1

I m not sure whether it is recommended or not but you can make use of the JavaScript Engine. Find below a trick :

char op = '*';
String operand1= "10";
String operand2 = "20";
ScriptEngineManager scm = new ScriptEngineManager();
ScriptEngine jsEngine = scm.getEngineByName("JavaScript");

System.out.println(jsEngine.eval(operand1+op+operand2));

This will give you 200.0

shikjohari
  • 2,278
  • 11
  • 23
  • @EdwardKenway I'd be staggered if this is the best way to achieve your *underlying goal*. Please [read this](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) document, then come back to us and describe what you're actually trying to do. – Duncan Jones Mar 18 '15 at 13:01
0

One way to do this without using if/else or switch is by defining an enum:

enum Operator {
  PLUS('+') {
    @Override public int apply(int a, int b) { return a + b; }
  }, TIMES('*') {
    @Override public int apply(int a, int b) { return a * b; }
  }, ... ;

  static final Map<Character, Operator> map = buildMap();
  final char ch;

  private Operator(char ch) { this.ch = ch; }

  public abstract int apply(int a, int b);

  static Map<Character, Operator> buildMap() {
    Map<Character, Operator> map = new HashMap<>();
    for (Operator op : Operator.values()) {
      map.put(op.ch, op);
    }
    return map;
  }
}

and then use this to process your inputs:

Operator.map.get(ch).apply(leftOperand, rightOperand);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I'm not sure why you need an enum here. `Operator` could be an interface and you'd lose no features that I can see. Ultimately you're just building a map of things which operate on two integers. – Duncan Jones Mar 18 '15 at 13:24
  • 'One way'. Sure, define an interface if you'd like, I just think that `enum` makes it easier to do things like know all of your operators simply. Arithmetic operators are probably not something you will need to add or remove at runtime. – Andy Turner Mar 18 '15 at 13:27
0

Following are the ways

  • ScriptEngineManager
  • with Java lexers and parsers
  • BeanShell interpreter

Since its already answered You can get more details of these in Evaluating a math expression given in string form

Community
  • 1
  • 1
Om.
  • 2,532
  • 4
  • 22
  • 22
0

If you really must do this, the binary functions introduced in Java 8 make this quite elegant code:

Map<Character, IntBinaryOperator> operators = new HashMap<>();    
operators.put('+', (x, y) -> x + y);
//...

You use this map like this:

char ch = '+';
int result = operators.get(ch).applyAsInt(10, 20);

Although perhaps this isn't what you're looking for, since it requires you to map '+' to an operation.

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • This limits us to a specific number of operations – Unaware Cub Mar 18 '15 at 13:30
  • @EdwardKenway I ask again - why are you doing this? If you can explain that, answers might get better... You are always going to be limited to some set of inputs, even if you are intending to execute the input as Java code. – Duncan Jones Mar 18 '15 at 13:37