I need to split a string that is going to be the input of a calculator. I want to split it into two arrays using the split function in java. The first array should only have the numbers of the string (this includes numbers with decimal places). The second should only include the mathematical operators like “+-*/”. Currently this is my code:
String[] numbers = s.split("[[+-/*]&&[^.]]");
String[] operators = s.split("[^*/+-]");
I seem to be able to split the string into the seperate numbers array perfectly fine (even with decimal points), but I can’t seem to be able to split the string into the separate operators if there is a decimal point anywhere with the numbers (it works otherwise though).
Ex: I want “9.0/3+2.3” to become {/,+}.
What should I change my regex (for the second line of code) to be to get my desired output? Everything I’ve tried hasn’t worked.
Try this: