Removing String (java) | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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