Passing and returning StringBuilders: Java

I was sent to this site by a friend. I like that I was able to look at some of these threads before I started this thread. Unfortunately, I have not seen anything about this particular problem.

I am having a problem passing StringBuilders from object to object in Java. I do not know what the problem is, and I got lost. I can transfer and return data all day. With StringBuilders this just doesn't work. I think it’s really simple, that I’m just too upset to see.

I have a homework that asks me to declare 3 Stringbuilders with my first name, first name and last name. No problems.

He also wants 3 objects to perform different name formats. The formatting part is simple. I can’t understand how to return work to the main thing.

Here is a snippet:

public class Builder
{
   public static void main(String[] args)
   {
      StringBuilder str1 = new StringBuilder("John");
      StringBuilder str2 = new StringBuilder("Que");
      StringBuilder str3 = new StringBuilder("Doe");

      ???  = entireName(str1, str2, str3);
   }

  public static String entireName(StringBuilder s1, StringBuilder s2, StringBuilder s3)
  {
        System.out.print(s1);
        //insert.s1('4', ' ');//format stuff, not really necessary (not a problem).
        Ststem.out.print(s2);
        //etc..
        System.out.print(s3);
        return ????;
   }
}

, , , . . , stringbuilder. . . , .

. !

+3
1

, , s1.toString()

public static String entireName(StringBuilder s1, StringBuilder s2, StringBuilder s3)
  {
        System.out.print(s1.toString());
        //insert.s1('4', ' ');//format stuff, not necessary (not a problem)
        System.out.print(s2.toString());
        //etc..
        System.out.print(s3.toString());
        return s1.append(s2).append(s3).toString();
   }

, :

public class Builder
{
   public static void main(String[] args)
   {
      StringBuilder str1 = new StringBuilder("John");
      StringBuilder str2 = new StringBuilder("Que");
      StringBuilder str3 = new StringBuilder("Doe");

      String s = entireName(str1, str2, str3);

      System.out.println(s);
   }

   public static String entireName(StringBuilder s1, StringBuilder s2,
            StringBuilder s3)
   {
        // debugging removed
        return s1.append(s2).append(s3).toString();
   }

}
+3

All Articles