0

I want to know what mistakes i have done in code and where i could use more efficient method.

I have made a calculator which receive data as e.g. 10+20 and recognize numbers than print desire output. https://code.sololearn.com/cmE0eeO8wfw6/?ref=app

11th Jul 2020, 2:25 PM
Ikram Arshad
Ikram Arshad - avatar
10 Answers
+ 1
// with regex import java.util.regex.*; public class Program { public static void main(String[] args) { String[] input = {"10+20","11-21","12*22","13/23"}; for (var example : input) { Matcher m = Pattern.compile("(\\d+)([+\\-*/])(\\d+)") .matcher(example); m.find(); System.out.println(m.group(1) +" "+ m.group(3) +" "+ m.group(2)); } } }
11th Jul 2020, 5:28 PM
zemiak
+ 2
You're obviously coming from C*. You don't need these string arrays at all in Java. Your code can be much simpler, when you just get the input and split it on the operators.
11th Jul 2020, 2:31 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Oh, and you should prefer the Integer.valueOf() method instead of parsing.
11th Jul 2020, 2:32 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
In fact Java is less tricky 😉
11th Jul 2020, 2:35 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
It working correctly.. What the problem you getting..?
11th Jul 2020, 2:41 PM
Jayakrishna 🇼🇳
+ 1
You can solve any problem many ways.. Here, yes you can use split function or some script language have direct evaluate function, so you can import and use in your code or you can write your own evaluation method.. Or just read input by space separating by next() method and convert to required type... Edit: For splitting input: if(j=='+') String s[] = str.split("+") s[0],s[1] contain values, convert to int and add.
11th Jul 2020, 3:00 PM
Jayakrishna 🇼🇳
0
Yeah i just moved from cpp to java and i think java is a little tricky
11th Jul 2020, 2:35 PM
Ikram Arshad
Ikram Arshad - avatar
0
Can you explain how can i split input on operators?
11th Jul 2020, 2:35 PM
Ikram Arshad
Ikram Arshad - avatar
0
Use split-method: string.split("<whatever>")
11th Jul 2020, 2:37 PM
Sandra Meyer
Sandra Meyer - avatar
0
I want to know which other method could be used for this program
11th Jul 2020, 2:44 PM
Ikram Arshad
Ikram Arshad - avatar