aspose file tools
The moose likes Beginning Java and the fly likes Problem in String Buffer - incrementing char in string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Problem in String Buffer - incrementing char in string" Watch "Problem in String Buffer - incrementing char in string" New topic
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
    
  15

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
    
  15

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
    
    4
Get the entire String as a char[] and use the ++ operator?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
. . . and Wouter has already told you to use StringBuilder, not StringBuffer.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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
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
    
    4
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Problem in String Buffer - incrementing char in string
 
Similar Threads
Threads - K&B Exercise 9-2 -
KATHY SCJP 1.6 : THREADS, exercise 9-2. Is my code fine?
Synchronized block of code
Threads and synchronisation
K&B Chapter 9 exercise 9-2 page 738.