Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

StringBuffer's into Array

Posted on 2000-03-12
6
1,456 Views
Last Modified: 2008-02-26
I want to put 5 different Stringbuffer's into an Array that i can use the Index 0 to 4.
Can you help me?
0
Comment
Question by:MartinSchuster
6 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 2610039
instead of using array use vector


StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
StringBuffer sb3 = new StringBuffer();
StringBuffer sb4 = new StringBuffer();

Vector v = new Vector();
StringBuffer StrBuff[];
sb1.append(some data);
sb2.append(some data);
sb3.append(some data);
sb4.append(some data);

v.addElement(sb1);
v.addElement(sb2);
v.addElement(sb3);
v.addElement(sb4);

so while retrieving u have to do

for(int i = 0 ;i<v.size();i++)
{
   
  StrBuf[i] =(StringBuffer) v.elementAt(i);
   
}

RJackman
0
 

Accepted Solution

by:
darrenc earned 400 total points
ID: 2610981
Hi Martin,

The easiest way to do this is to simply initialise a StringBuffer array and then populate it.

Here's an example

*** test.java ***
import java.util.*;

public class test {
   public static void main(String args[]) {
        StringBuffer buffArray[] = new StringBuffer[5];
        buffArray[0] = new StringBuffer();
        buffArray[1] = new StringBuffer();
        buffArray[0].append("Hello");
        buffArray[1].append("Hi");
        System.out.println(buffArray[0].toString());
        System.out.println(buffArray[1].toString());
   }
}

*** end test.java ***

Hope that helps
Darren
0
 
LVL 1

Expert Comment

by:techtime
ID: 2611032
And what is the problem with that?
Though rjackman is right here is the code that seem to work & be exactly what you've asked for:

StringBuffer sbA[] = new StringBuffer[5];

for(int i=0;i<5;i++)
 sbA[i] = new StringBuffer();

for(int i=0;i<5;i++)
 sbA[i].append("Here is sb#"+i);

for(int i=0;i<5;i++)
  System.out.println(sbA[i]);

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Expert Comment

by:darrenc
ID: 2614048
Hi,

Yes you are right ... the original code fragment from rjackman would work (sorry if anyone thought I meant that the original comment was wrong)

Darren
0
 
LVL 1

Expert Comment

by:techtime
ID: 2614794
Well dear darrenc my comment had nothing to do with yours in fact I've posted it before you... Alas, the network says its word.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2751166
Martin, this looks like a duplicate of question http://www1.experts-exchange.com/Computers/Programming/Languages/Java/Q_10309649.html.

Did any of the comments on either question help you?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

839 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