• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem in String Buffer - incrementing char in string

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get the entire String as a char[] and use the ++ operator?
 
Campbell Ritchie
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic