Good day, tell me how to draw a symbol from Char, for example, I want to

char x = '+'; char a = 1; char b = 3; System.out.println(a + x + b); 

How to do that counted? It feels like something else is going on.

  • if you want "to count," then use numeric variables, not characters. Let's say replace char to int - DreamChild
  • And how to transfer what operation to apply? If we allow a person to enter 2, then +, and 3, each number is read and entered into a variable, but how plus to pull it out so that the plus sign could not be found. - Stee1House
  • Plus, and other symbols of the operation can be used, as indicated in the answer @Free_ze - Rams666
  • I already understood, I just wanted to find out for common development, is there something else? Or only the number can convert char and the code of the actual character. - Stee1House
  • You can look towards [ScriptEngine] [1] - [use case] [2]. [1]: docs.oracle.com/javase/6/docs/api/javax/script/… [2]: stackoverflow.com/questions/11577259/… - Rams666

3 answers 3

Like that:

 static public int calculate(char op, int first, int second) { switch (op) { // ... case '+': return first + second; // ... } } // где-то там int first = 3; int second = 2; char operator = '+'; int result = calculate(operator, first, second); System.out.println(result); 
  • Thank you, can it be like that, but there are still ways? - Stee1House
  • one
    @steelhouse As I already mentioned, the string in the operation is not translated directly in any way (you can call the compiler on the fly, but this is a perversion). Perhaps the Java Reflection API and its Method class can help you choose the method you need on the go. docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html - free_ze

So the character code '+' 43, here and get 47 in the answer.

  • Well, I understood how to make + be +, I just can not understand. Maybe there is no char to be used, but what the hell knows. Not yet reached it probably. - Stee1House
  • 2
    @steelhouse Java is not a scripting language, the string does not directly turn into an operation, i.e. There is no equivalent of eval () from javascript here (although by means of reflection you can find a method by name and call it). - free_ze
  • Thank you, I'm just new, I ask, maybe something is missing. - Stee1House

To get a numeric value from a variable of type char:

  char a = '1'; int int_a = Integer.parseInt(String.valueOf(a));