Solved

StringBuffer's into Array

Posted on 2000-03-12
6
1,454 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 40
servlet example issue 6 38
java stored proc example 9 29
eclipse console opening separately 2 16
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

785 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