Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replacing particular characters in a string with...

 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that Strings are immutable, and calling methods on them won't change them.
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol geez, i cant believe i didn't catch that...

thanks,


Justin
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic