John Jai wrote:Post your changed code for further assistance.
I have changed only one line in the code.
is changed to
I am confused how to proceed. Guide me with the steps
posted 7 years ago
Christophe Verré wrote:Just in case, when I said that you may prefer using StringBuilder, I didn't mean that this would fix your problem. Creating a new StringBuilder in a loop will still erase all previous content.
ya i have changed that. thank you. Can you tell me with little more explanation, where to use StringBuffer and StringBulider ?
StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).
So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.
As per the issue, if you had instantiated StringBuffer onject before while loop, you code would have generated the desired output