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
10 odpowiedzi
+ 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));
} } }
+ 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.
+ 1
Oh, and you should prefer the Integer.valueOf() method instead of parsing.
+ 1
In fact Java is less tricky 😉
+ 1
It working correctly.. What the problem you getting..?
+ 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.
0
Yeah i just moved from cpp to java and i think java is a little tricky
0
Can you explain how can i split input on operators?
0
Use split-method: string.split("<whatever>")
0
I want to know which other method could be used for this program