+ 7

Removing String (java)

Is there any possible way to remove the string? For example: String name = "1 + 1"; If( nam == 2){ //I want this like to act like this 👉 1+1 == 2; System.out.println("hello"); } Is there any way to change the data type from String to void? ___________________________________ I tried like this this but failed lol... 👇 void num = System.out.print("1+1"); And void num = System.out.print("1 " + " 1"); ______________________________________ Is that even possible by anyway? If so please tell me :) Thank you programmers! :)

20th Feb 2020, 12:43 AM
KingDaniel2004
KingDaniel2004 - avatar
14 odpowiedzi
+ 5
Mr. 🍊range , As Avinesh said you can use switch but that'll not be enough to evaluate expressions in proper order. In order to maintain proper evaluation order you need stack. For example in 5+3*6 should be 23 not 48. Stack helps you to evaluate according to operator precedence. I have used stack to evaluate postfix expression (oparands are digit only 0-9) . //but you need infix. https://code.sololearn.com/cG9uKJ8aT7so/?ref=app
20th Feb 2020, 4:34 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 5
👑 Prometheus 🇸🇬 To ask the user to type their question for example "3 + (8*3)" then calc their question... I don't find any other way to solve my problem 😂 I know there a way to solve it... but I don't know how😂
20th Feb 2020, 1:26 AM
KingDaniel2004
KingDaniel2004 - avatar
+ 5
Avinesh Yes that will work but...how do u convert arithmetic operations? For example if they says "40 - 3" how do u get that minus from the string?
20th Feb 2020, 3:55 AM
KingDaniel2004
KingDaniel2004 - avatar
+ 5
Mr. 🍊range , see this example by John sir. It does exactly what you asked. https://code.sololearn.com/ctEXGtkw5ahO/?ref=app
20th Feb 2020, 4:10 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 5
🇮🇳Omkar🕉 and Avinesh thank you both for ur help :)
20th Feb 2020, 4:42 AM
KingDaniel2004
KingDaniel2004 - avatar
+ 4
But why would you store a System.out.print()?
20th Feb 2020, 1:16 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
Mr. 🍊range you can probably make use of switch case which can contain all the operators that can be mapped.
20th Feb 2020, 4:27 AM
Avinesh
Avinesh - avatar
+ 3
🇮🇳Omkar🕉 since he mentioned only two operands separated by an operator I suggested him to go with switch directly. But for more number of operands and operators, precedence comes into picture just like you said. Nice example there👍
20th Feb 2020, 4:38 AM
Avinesh
Avinesh - avatar
+ 2
Apart from using eval you can also use stack. Just search for `infix expression evaluation using stack` on internet.
20th Feb 2020, 4:14 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
For example Scanner sc = new Scanner(System.in); String name=sc.nextLine().replaceAll(“ “, “”); int res=0; if (name.contains(“+”)){ String[] arr = name.split(“+”); for (int i=0; i<arr.length; i++{ int res+=Integer.parseInt(arr[i]); } } System.out.println(res); If user enter 32 + 6 you will replace all whitespaces String name = “32+6” arr[0]=32 arr[1]=6 res=38;
20th Feb 2020, 7:08 PM
Николай Макаренко
Николай Макаренко - avatar
0
Take string input and convert them to integers for performing any operation.
20th Feb 2020, 3:49 AM
Avinesh
Avinesh - avatar
0
I dont know what you are creating but you can try this code see if the format works for you. int a = 1; int b = 1; String Name = Integer.toString(a+b); int name = Integer.parseInt(Name); if(name==2){ System.out.println("hello");
20th Feb 2020, 7:42 AM
Felix Munyaradzi Manamike
Felix Munyaradzi Manamike - avatar
- 1
No removing
21st Feb 2020, 7:15 AM
Java
Java - avatar
- 1
Int a=1; Int c=1; String name= int.toStrinh(a+b); int name=int.parselnt(name); if (name==2){ System.out.println("hello");
21st Feb 2020, 7:18 AM
Java
Java - avatar