• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replacing particular characters in a string with...

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol geez, i cant believe i didn't catch that...

thanks,


Justin
 
Have you no shame? Have you no decency? Have you no tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic