1

How would you convert the functions of a String Buffer to a String Builder. I know the two are very similar but I can't change my StringBuffer sb = new StringBuffer(); to StringBuilder sb = new StringBuilder(); without getting an abundance of error messages.

Here is a little code I made:

StringBuffer sb = new StringBuffer();      //Create a new String Buffer with nothing in it
for (int n = 0; n < 1; n++) {
   sb.append("Billy ");                //Add  (first name) 
   sb.append("Scranner");              //Add  (surname)
   System.out.println(sb);             //Print the string buffer
   sb.insert(5," D");                  //Insert  " D" at the 4th character
   System.out.println(sb);             //Print the String Buffer with the middle initial
   sb.delete(5, 7);                    //Delete the character line from 4 to 6  
   sb.reverse();                       //Reverse the String Buffer 
   System.out.println(sb);             //Print the reversed String Builder

I apologize if my question seems unclear, TLDR; I would like to know how to obtain the same output that I get from this program using a String Builder instead of the String Buffer.

Here is what the program outputs:

Billy Scranner

Billy D Scranner

rennarcS ylliB

EDIT: This is the output I get when I replace Buffer with Builder:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
     The method append(String) is undefined for the type StringBuilder
     The method append(String) is undefined for the type StringBuilder
     The method insert(int, String) is undefined for the type StringBuilder
     The method delete(int, int) is undefined for the type StringBuilder
     The method reverse() is undefined for the type StringBuilder
21
  • 1
    Would you like to elaborate on these error messages? Commented Feb 19, 2017 at 19:08
  • @JoeC I'll add it to the post, more readable that way Commented Feb 19, 2017 at 19:11
  • I don't think that's what your program outputs. There's new lines in there and you reversed the entire buffer, not just your name Commented Feb 19, 2017 at 19:12
  • No, according to the Javadoc they are definitely defined. Can you please create a Minimal, Complete, and Verifiable example that would allow me to reproduce this? Commented Feb 19, 2017 at 19:13
  • This is probably a dumb question, but could you show me how to import String builder ? @Kayaman Commented Feb 19, 2017 at 19:16

2 Answers 2

3

There are no errors in replacing StringBuffer sb = new StringBuffer(); to StringBuilder sb = new StringBuilder();

    import java.lang.StringBuilder;

    StringBuilder sb = new StringBuilder();
    sb.append("Billy "); 
    sb.append("Scranner"); 
    System.out.println(sb); 

    sb.insert(5, " D");
    System.out.println(sb); 
    sb.delete(5, 7);

    sb.reverse(); 

    System.out.println(sb);

Output:

Billy Scranner
Billy D Scranner
rennarcS ylliB
Sign up to request clarification or add additional context in comments.

3 Comments

you should add the output as comments in your code
@MasterYushi added output.
@MasterYushi output will be same as stated in the question. Why put that again? Answer covers the answer part. +1
1

You should have the exact same output using StringBuffer and StringBuilder with the code you present.

The difference between StringBuffer and StringBuilder is that StringBuffer seems overkill as it is thread-safe.

If you get errors, might be related to:

  • thread issues
  • Development environment not being correctly set
  • Typos
  • File format (sometimes you copy/paste code from a source that has invisible characters preventing you from compiling the file correctly).

From what I see in the comments, you have invisible end of lines or characters that are not in the correct encoding of your platform.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.