Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While replacing char of soap response then it throws error

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can anybody please let me know that why its throwing error for:
str=str.replace("<", "<");
str=str.replace(">", ">");
str9+=str;



That's giving the following error:

 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First let's start with the warning. You shouldn't use classes from packages that start with com.sun, sun or sunw. These classes are for JVM internal use only, and can be modified or removed as Oracle feels fit. Check out Apache Commons instead, they have a base64 class in their Codecs library.

Right, to the errors. String has method replace(CharSequence, CharSequence). StringBuilder and StringBuffer only have method replace(char, char) - there is no method for replacing full Strings. The solution in this case is quite simply - replace in the line first, then append the line:
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Spoor the problems fixed but how can I generalize the following code because its looking odd to use every time line=line.replace("this","toThis")

Thanks again
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a map that you initialize once, then iterate over:
Because the map won't change, it's best to make it static, final and immutable:
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks from the bottom of my heart
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic