Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Creating StringBuffer objects

How do I go about creating a new StringBuffer object inside a class that holds three initialized StringBuffers?
0
RgrWalker
Asked:
RgrWalker
1 Solution
 
arnon81Commented:
Hi,
I am not sure about your question but maybe
StringBuffer a = new StringBuffer() ;

0
 
CEHJCommented:
>>inside a class that holds three initialized StringBuffers

What does that have to do with it btw?
0
 
CEHJCommented:
If you mean you want to fill a new one with the others, you can do

StringBuffer newOne = new StringBuffer();
newOne.append(bOne).append(bTwo).append(bThree); // where 'bX' is a StringBuffer
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
RgrWalkerAuthor Commented:
public class EventStringBuffer
{
public static void main (String[] args)
{
StringBuffer eventString = new StringBuffer("Buffer Handlers Incorporated");
int aCapacity = eventString.capacity();
System.out.println("The capacity is  " + aCapacity);

StringBuffer philosophyString = null;
philosophyString = new StringBuffer("Dedicated to making your event a most memorable one");
int bCapacity = philosophyString.capacity();
Syste.out.println("The capacity is " +bCapacity);

eventString.setLength(50);
System.out.println("The eventString is " + eventString + "end");

philosophyString.setLength(30);
System.out.println("The philosophyString is " + philosophyString);
}
}


This is the one of the examples I'm finding in my book that could relate to what I'm trying to do.

I'm trying to make heads and tails of this and relate it to what I need.
0
 
RgrWalkerAuthor Commented:
Ok, this is what I've come up with going off the above example.  First of all the question that I needed to answer.

Create a class that holds three initialized StringBuffer objects: your first name, middle name, and last name.  Create three new StringBuffer objects as follows:

o  An object named EntireName that holds your three names, seperated by spaces

o  An object named LastFirst that holds your last name, a comma, a space, and your first name, in that order

o  An object named Signature that holds your first name, a space, your middle initial (not the entire name), a period, a space and your last name.

Based off that..............................

This is what I came up with.................

public class Buffer
{
public static void main (String[] args)
{
StringBuffer EntireName =
new StringBuffer("My Entire Name goes here");
int aCapacity = EntireName.capacity();
System.out.println("The capacity is " + aCapacity);

StringBuffer LastFirst =
new StringBuffer("Last name then First name");
int bCapacity = LastFirst.capacity();
System.out.println("The capacity is " +bCapacity);

StringBuffer Signature =
new StringBuffer("My Name with the Middle Initial");
int cCapacity = Signature.capacity();
System.out.println("The capacity is " +cCapacity);


EntireName.setLength(22);
System.out.println("The EntireName String is " +EntireName);

LastFirst.setLength(17);
System.out.println("The LastFirst String is " +LastFirst);

Signature.setLength(19);
System.out.println("The Signature String is " +LastFirst);
}
}


My question to you is..............

Is this correct?
0
 
imladrisCommented:
Is it correct? Well, I would expect that code to compile, and do what is listed.

As to whether it relates to what you need...., well you haven't really told us what you need.
0
 
RgrWalkerAuthor Commented:
As to whether it relates to what you need...., well you haven't really told us what you need.


????

Create a class that holds three initialized StringBuffer objects: your first name, middle name, and last name.  Create three new StringBuffer objects as follows:

o  An object named EntireName that holds your three names, seperated by spaces

o  An object named LastFirst that holds your last name, a comma, a space, and your first name, in that order

o  An object named Signature that holds your first name, a space, your middle initial (not the entire name), a period, a space and your last name.


That is what I need
0
 
objectsCommented:
you want something more like:

public class Buffer
{
public static void main (String[] args)
{
StringBuffer firstname = "Homer";
StringBuffer middlename = "Jay";
StringBuffer lastname = "Simpson";

StringBuffer EntireName = firstname+" "+middlename+" "+lastname;
int aCapacity = EntireName.capacity();
System.out.println("The capacity is " + aCapacity);

StringBuffer LastFirst = lastname+", "+firstname;
int bCapacity = LastFirst.capacity();
System.out.println("The capacity is " +bCapacity);

StringBuffer Signature = firstname+" "+middlename.charAt(0)+". "+lastname;
int cCapacity = Signature.capacity();
System.out.println("The capacity is " +cCapacity);


System.out.println("The EntireName String is " +EntireName);

System.out.println("The LastFirst String is " +LastFirst);

System.out.println("The Signature String is " +LastFirst);
}
}
0
 
MogalManicCommented:
The StringBuffer class is created so that your programs can efficiently build and process Strings.  90% of the time you will just use Strings, and use StringBuffers to build them.  The reason for this is that the String class is immutable (This means that once set it cannot be changed).  Look at the following code:
String a="Hello, world";
a="goodbye, world";

The above lines create two strings.  First 'a' points to "Hello, world", then 'a' is replaced with a NEW string "goodbye, world" (The 2 strings allocate 2 separate places in memory.  StringBuffers allow you to build complex Strings.  For example:

StringBuffer a=new StringBuffer("hello");
a.append(',');
a.append(" ");
a.append("world)"

String b=a.toString();

The above lines creates a StringBuffer initialized to "hello".  Then appends a ',', then space (" "), then "world".  Finally the String b is populated with "hello, world".  The code can be abbrievated to:
String b="hello"+","+" "+"world";
The Java compiler will internaly convert the String math (+) to successive calls to a temporary StringBuffer appending the intermediate results.

To do your example, it would be simalar to yours and objects:
public class Buffer
{
public static void main (String[] args)
{
  String first, middle, last;

  first="Bob";
  middle="Alfred";
  last="Smith";

  StringBuffer EntireName = new StringBuffer();
  entireName
    .append(first)
    .append(' ')
    .append(middle)
    .append(' ')
    .append(last);
  int cCapacity = EntireName.capacity();
  System.out.println("The EntireName capacity is " +cCapacity);

  StringBuffer signature = new StringBuffer();
  signature
    .append(first)
    .append(' ')
    .append(middle.charAt(0))
    .append(' ')
    .append(last);

  int cCapacity = Signature.capacity();
  System.out.println("The Signature capacity is " +cCapacity);

  System.out.println("The EntireName String is " +EntireName);
  System.out.println("The Signature String is " +Signature);

  System.out.println("The LastFirst String is " +last+", "+first);

  System.out.println("The middle Name String is " +middle);
}
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now