| Author |
Problem in String Buffer - incrementing char in string
|
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
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 :
|
SCJP 6. Learning more now.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
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
|
Mohamed Sanaulla | My Blog
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
And you probably want a StringBuilder not a StringBuffer.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
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
Joined: Sep 08, 2007
Posts: 2928
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
Get the entire String as a char[] and use the ++ operator?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
. . . and Wouter has already told you to use StringBuilder, not StringBuffer.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
This code will be used in threads, so i used StringBuffer...that brings another post....
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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.
|
 |
 |
|
|
subject: Problem in String Buffer - incrementing char in string
|
|
|