Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with replaceAll() method  RSS feed

herman ginting
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey folgs
i have a problems with replaceAll(“”,”") method.
i have a file named with “synonimreplacer.txt”
the file contain of list of word as below

and i have a colection dataset from database that contains some sentence. the program is about to search matching term in each row and replace it with its subtitute,
if the program found ‘min’ in current row than it would be replace by ‘admin’
and so on
i have successfully read the synonimreplacer.txt using File() and Scanner,
and take each row from synonimreplacer.txt and save it into a string variable
than i split it into two string term and replacer.
and the string from the database is modified by replaceAll(term,replacer) methode, but there is no change at all.

ive try it manually like below
newTweet = tweet.replaceAll(“min”,”admin”);
and it works
i cant figure it out whats different if i collect all the term and its subtitutes in a text file

im stuck.
looking forward for your folgs
kind regards

Steve Luke
Posts: 4181
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't provided enough information to answer the question. A few things to try out:

- Print out the source string, the term, and the replacer. See what each of those values are and maybe that will help you figure out what is going on.

- Start from scratch. Create a list of Strings to call the replaceAll on. Create a list of Strings in the format "term=replacer". You now have a much simpler system to test on. If you can duplicate the problem this way, it might be easier to diagnose. If you can't duplicate the problem here then start adding on some of the real-world complexity. First, instead of starting with a list of Strings in the "term=replacer" format, read the values from your file. If you duplicate the problem you know what code must be causing the problem (the file read stuff). Then add on the database operations and see if you reproduce the problem - again, if you do you know where the problem comes from. Finally, if you don't reproduce the problem, you now have functional code you can put back into your original app to make it work.
Piet Souris
Posts: 1984
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Herman,

I was faced with the same problem last week, but I think the solution is very simple.

string.replaceAll("a". "b") doesn't work as you saw, but that is because the result of this operation is put into a new string, and not the original string.

That's why your manual try was successful.

In short:

does not change s, but


Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!