How do you separate an arithmetic expression with space, example i have this expression 22+(33*1) i want to split them with space between operators in the same string (returning a String) like 22 + ( 33 * 1 ) by the way im using StringTokenizer
Asked
Active
Viewed 692 times
0
-
1Have you tried anything? – juergen d Nov 22 '12 at 23:56
-
yep tried using StringTokenizer with and separated "" with " " but its result is 2 2 + ( 3 3 * 1 ) it is seperated every digit, i just want to seperate it per operand like the one i stated above – lemoncodes Nov 22 '12 at 23:58
-
an inefficient way would be to replace every token with itself followed by a space (using `String.replaceAll`) then split the string with a regex of `\x20+` (using `String.split`). Quick and dirty, but might meet your requirements. – Nathan Ryan Nov 23 '12 at 00:03
-
Why? The meaning is the same either way. Or is this a step towards scanning and parsing the expression? In which case *that* is the question you should ask, and adding spaces has nothing to do with it. – user207421 Nov 23 '12 at 00:11
-
to put it simply i just want to format the string like the one i said above – lemoncodes Nov 23 '12 at 01:03
-
@lemoncodes I suggest that either the requirement is wrong or you have misunderstood it. Scanning and parsing do not require insertion of redundant spaces. – user207421 Nov 23 '12 at 02:56
3 Answers
1
Your rule is simpler then you think. Probably correct java:
expr = expr.replaceAll('+', ' + ')
.replaceAll('-', ' - ')
.replaceAll(')', ' ) ')
//etc
.replaceAll(' ', ' '); //remove any double spaces added.

Eric
- 95,302
- 53
- 242
- 374
-
Does not work when there are already spaces in the original expression. you would introduce additinal ones. (Which could be eliminated afterwards) – AlexWien Nov 23 '12 at 00:54
-
this is is the closes to what i did, although not really the same code but the idea is there – lemoncodes Nov 23 '12 at 01:13
0
That is Not Easy. You have to parse the Expression and
Build up a tree of tokens. You Finally behave like
A parser. Then you recreate the Expression and insert a Space between
Each element.
try to search for parsing arithmetic expressions

AlexWien
- 28,470
- 6
- 53
- 83
-
Once he has the tree it's hard to see why he would then want to go back to a linear string at all. Strange question. – user207421 Nov 23 '12 at 00:15
-
-
im not building a tree, im just separating each operand and operator with a whitespace, building a tree is for the later part of my program, im just building it up ready for the tree part, to put it simply i just want to speratre operand and operator with a whitespace basically its just string not any other strcuture – lemoncodes Nov 23 '12 at 00:55
-
1Inserting spaces will not help your later part. Skip your question and start with the later part. – AlexWien Nov 23 '12 at 00:56
-
-
Then either you or the person who wrote the req is wrong! Dont hesitate to doubt the req. – AlexWien Nov 23 '12 at 01:14
-
hehe naahh its part of the training actually, how to cope with hardheaded clients :D – lemoncodes Nov 23 '12 at 01:23
0
You can use multiple String.replaceAll
statements to replace operators with operators with spaces.

ipavlic
- 4,906
- 10
- 40
- 77
-
Just to clarify, you don't *need* multiple `replaceAll` expressions. It can be done with a single one, using a bit of regex and replacement magic. – Nathan Ryan Nov 23 '12 at 00:09