Solved

Creating StringBuffer objects

Posted on 2004-09-15
9
273 Views
Last Modified: 2013-12-29
How do I go about creating a new StringBuffer object inside a class that holds three initialized StringBuffers?
0
Comment
Question by:RgrWalker
9 Comments
 
LVL 2

Expert Comment

by:arnon81
ID: 12068588
Hi,
I am not sure about your question but maybe
StringBuffer a = new StringBuffer() ;

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12068656
>>inside a class that holds three initialized StringBuffers

What does that have to do with it btw?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12068696
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
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!

 

Author Comment

by:RgrWalker
ID: 12069182
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
 

Author Comment

by:RgrWalker
ID: 12069457
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
 
LVL 16

Expert Comment

by:imladris
ID: 12069637
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
 

Author Comment

by:RgrWalker
ID: 12069827
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12071326
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 12071659
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 48
ejb entity bean example issue 2 25
What is the use of Forwarding Class in java 1 34
Java array 21 78
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question