I am making a calculator in Java. The problem is that I am trying to find a match that, given a string, takes only the second operand (operations can be
, -
, *
and /
).
Example strings inputs:
21 574
573 * 1
89 - 4312
23 / 23
-1 4
-3 45
Corresponding strings outputs:
574
1
4312
23
4
45
Does anyone know of a match to do this?
CodePudding user response:
As I have already provided within the comments section of your post the following will split out the values(s) you want:
System.out.println("21 574".split("\\s*(\\ |\\-|\\*|\\/)\\s*")[1]);
This is only good for unsigned (positive) numbers since the minus (-) is also used for the sign of a negative number (example -574). If you want also handle negative values then a slight change will be needed to the regular expression. We need to add a whitespace after the '-' character, for example:
"\\s*(\\ |\\- |\\*|\\/)\\s*"
^
Space
This then means, any equation that contains subtraction must also contain a whitespace after the minus operator. This works well if the equations are as follows: 4 -6
. This will still be fine even if there are no whitespaces between digits and operators, for example: 4 -6
. But this changes entirely if the operator in the previous equation was a minus operator instead of the plus operator, for example: 4--6
. This equation would actually generate a ArrayIndexOutOfBoundsException. This is obviously no good.
To get around this particular situation, we need to separate the two minus characters (--
) and to do this we can use the String#replace() method, for example:
System.out.println("4--6".replace("--", "- -").split("\\s*(\\ |\\- |\\*|\\/)\\s*")[1]);
This will output: -6
.
There is going to come a time (very soon I'm sure) where even this or any other little shortcut is not going to be good enough and you will need to actually parse the equation. Parentheses and braces play a big part in mathematical equations and you are going to need something to parse all that out. Regular Expressions may not be quite good enough for this sort of thing.
Below I provide a quick method to split equations into individual components which may or may not contain brackets, braces, negative numbers, etc. Perhaps this can be used as a starting point for creating your own Equation Parser (or find and utilize one that has already been developed).
/**
* This method will split a math equation string (even with various bracket
* types or nested brackets) into a String[] Array. Signed and unsigned
* integer or floating point values can be within the supplied mathematical
* equation.<br><br>
*
* Whitespaces are ignored during parsing and will not be contained within
* the returned String[] Array as an array element.<br>
*
* @param equation (String) The mathematical equation to parse into a
* String [] Array.<br>
*
* @return (Single Dimensional String[] Array)
*/
private String[] splitEquation(String equation) {
List<String> list = new ArrayList<>();
String number = "";
for (char c : equation.toCharArray()) {
if (c == ' ') {
if (!number.isEmpty()) {
list.add(number);
number = "";
}
}
else if (Character.isDigit(c) || c == '.' || (c == '-' && number.isEmpty())) {
number = Character.toString(c);
}
else if (!Character.isDigit(c) && !number.isEmpty()) {
list.add(number);
number = "";
list.add(Character.toString(c));
}
else {
list.add(Character.toString(c));
}
}
if (!number.isEmpty()) {
list.add(number);
}
return list.toArray(new String[list.size()]);
}
To use this method:
String[] parts = splitEquation("(4 (-3/2))");
System.out.println(Arrays.toString(parts));
The console window will display:
[(, 4, , (, -3, /, 2, ), )]