It's not a secret anymore!*
The moose likes Beginning Java and the fly likes remove repeated operators (+-/*) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "remove repeated operators (+-/*)" Watch "remove repeated operators (+-/*)" New topic
Author

remove repeated operators (+-/*)

anoop kerala
Greenhorn

Joined: Mar 03, 2010
Posts: 10
hi all.. how to remove the duplicate operators in a string?
for eg: a++b should give a+b
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
How many operators do you have ?


JDBCSupport - An easy to use, light-weight JDBC framework -
anoop kerala
Greenhorn

Joined: Mar 03, 2010
Posts: 10
the expression should be for example a=(a+b)-c and if user mistakenly give a=(a++b)--c we have to get the correct expression
a=(a+B)-c

thank you..
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
how about..




There might be a super simple regex though ...
anoop kerala
Greenhorn

Joined: Mar 03, 2010
Posts: 10
thanks a lot.. let me try this.. this is my first post and amazed by your fast reply..

will let you know once i try this..

regards,
lenin
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You could improve this example by making the operator array a parameter. This way you explicitly define what operators should be removed.
anoop kerala
Greenhorn

Joined: Mar 03, 2010
Posts: 10
yes.. your snippet addressed my req..

thanks..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Sebastian Janisch wrote:how about..




There might be a super simple regex though ...

Your replace option may not be the most efficient. You loop several times over the StringBuilder; indexOf starts from the start each time. You then do this for all 4 operators. You can improve performance by iterating over the StringBuilder once, deleting characters as needed:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
And welcome to the Ranch
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 173

If you want the method to coalesce any number of multiple operators (e.g. "+++" --> "+") then you need to put an 'else' in front of the index increment.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Sebastian Janisch wrote:
There might be a super simple regex though ...



How about....



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
anoop kerala
Greenhorn

Joined: Mar 03, 2010
Posts: 10
good morning all..
thanks for your help..

the regex by wong is very nice i will give some eg:,

1) a++b output -> a+b

2)a+++b output -> a+b

but

3) a+-b output -> a+-b , but what i want is a+b

4)a++-b output -> a+-b, but what i want is a+b itself.

regards,
lenin





Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You only asked about removing repeated operators. +- is not a repeated operator. Moreover, a+-b is (mathematically) not the same as a+b -- it is the same as a-b. That's an essential difference.
anoop kerala
Greenhorn

Joined: Mar 03, 2010
Posts: 10
yes i said repeated operators..

but i gave eg in that way, that was my mistake..

let me explain,

1) a+*b output is a+*b , but the operator is getting repeated.. i think you got what i mean..

and hope you will help me..

lenin
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18520
    
  40

Anoop Lenin wrote:and hope you will help me..


Your new requirement is actually *easier* than the previous one -- somewhat. Why don't you attempt to do it yourself? You can't learn if you don't try.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: remove repeated operators (+-/*)
 
Similar Threads
Unary Operators?
whats wrong with a+++++b ?
BitWise Operators in SCJP 5.0 ?
Auto-Increment
Bitwise and Bit Shift on exam?