Solved

Creating StringBuffer objects

Posted on 2004-09-15
9
268 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
Comment Utility
Hi,
I am not sure about your question but maybe
StringBuffer a = new StringBuffer() ;

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>inside a class that holds three initialized StringBuffers

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

Expert Comment

by:CEHJ
Comment Utility
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
 

Author Comment

by:RgrWalker
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:RgrWalker
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 75
countPairs challenge 7 57
pairs challenge 5 44
eclipse package explorer vs project explorer view 2 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now