aspose file tools*
The moose likes Beginning Java and the fly likes replacing particular characters in a string with... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replacing particular characters in a string with..." Watch "replacing particular characters in a string with..." New topic
Author

replacing particular characters in a string with...

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok say you have the string:

"((R))(SR)()()S"

I need to replace all the "S" and "R"'s with "()"

how do I go about doing this?

with out using String.replaceAll

cause I just couldn't get that darn method to work


Thanks,
Justin


You down with OOP? Yeah you know me!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Justin Fox:
...with out using String.replaceAll

cause I just couldn't get that darn method to work...

I think it would be easier to figure out how that darn method works before trying to write your own algorithm. What have you tried? And what seems to be the problem?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Keep in mind that Strings are immutable, and calling methods on them won't change them.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
nevermind, lol

I used String.replaceAll(substring a, string b);

I have two recursive methods that needed to call it,

and only had it in one...

so say i had this string at current outcome..

(S)RRR())S)

the actual outcome was (())RRR())())
because i only had the converting method in the S() function..


I figured it out...

here is the code:







Justin
[ September 23, 2006: Message edited by: Justin Fox ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok i modified the above Exp class to print to a file, but
for some reason i keep getting this error:




here is the modified code...



Please help me,

Justin
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
It's possible that an exception occurred where you created the PrintWriter.

If you put an SOP in the catch block, it may tell you what the problem is.

Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
On a more general note, it is a very bad practice to just swallow exceptions in this manner. Never ever do it or debugging the sort of errors you are seeing will be a serious headache. A System.out.println should be used at the very least, but preferably a e.printStackTrace() in the absence of a more complete logging framework.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
In the constructor for your Exp class you call S(String, int) before you instantiate your PrintWriter. At some point either the S() method or one of the RULE_X_() methods calls the PRINT() method and that tries to call a println() with the null PrintWriter.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok i realize i instatiated a null print writer...

but in the constructor i made it a non-null value..

so i shouldn't have a prob. using it... right?

i mean, this wasn't a part of the assignment, i just hate using

linux to make a script file...

so i was gonna output the "output" to a file and just print that up

Justin
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Justin Fox:
ok i realize i instatiated a null print writer...

but in the constructor i made it a non-null value..

so i shouldn't have a prob. using it... right?



The only problem is you're trying to use it before you instantiate it. Just try moving the creation of the PrintWriter to before you call the S() method.
[ September 26, 2006: Message edited by: Garrett Rowe ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
lol geez, i cant believe i didn't catch that...

thanks,


Justin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replacing particular characters in a string with...