• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in String Buffer - incrementing char in string  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to take a char of a string,increment it and replace it with the incremented char. eg A should be B. I used replace method, insert method of StringBuffer and got BA instead of B. How do i rectify this problem ? Here is my code :




Here is the incorrect output :

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaDoc for replace() wrote:Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of


So you must change your End Index in replace() method
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you probably want a StringBuilder not a StringBuffer.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
JavaDoc for replace() wrote:Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of


So you must change your End Index in replace() method


thank you, it worked. I thought that end and beginning should be the same because i only want to replace the first/zeroth char. So the method works like this : replace UP TO 'end index', NOT including end. Is that correct/complete ?

Can anyone suggest any other ways to do my program using StringBuffer only ?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
Can anyone suggest any other ways to do my program using StringBuffer only ?

Using replace is a straight forward way.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the entire String as a char[] and use the ++ operator?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and Wouter has already told you to use StringBuilder, not StringBuffer.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . and Wouter has already told you to use StringBuilder, not StringBuffer.

This code will be used in threads, so i used StringBuffer...that brings another post....
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code you've posted in your openings post it will only be used by one thread. If you've simplified the situation then it's good to inform us of that. For instance if you had mentioned in your openings post that the code is going to be used in a multithreaded environment then I wouldn't have made the suggestion for a StringBuilder.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote: . . . if you had mentioned . . . that the code is going to be used in a multithreaded environment then I wouldn't have made the suggestion for a StringBuilder.
Nor would I.
 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:I want to take a char of a string,increment it and replace it with the incremented char. eg A should be B. I used replace method, insert method of StringBuffer and got BA instead of B. How do i rectify this problem ?    Here is my code :




Here is the incorrect output :




Below is the corrected code - we can set the StringBuffer using setChatAt  method

 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refrain from posting complete code solutions in the beginners forum. The aim of this form is to help people to solve their own problems not to give them complete code solutions. But because this thread is 6 years old I will leave your solution here this time.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!