how to assign to String[] from StringBuffer in a loop?

Hi all,

publicclass Test{

public String[] getSJ()

{

String[]jg;

String[]jig;

String[]tg;

String[]result;

Date startTime;

StringBuffer buf =new StringBuffer();

int i;

int j;

int k = -1;

jg ={"g1","g2"};

for( i=0; i < jg.length; i++ )

{

jig ={"1","2","3"};

for( j=0; i < jig.length; j++, k++ )

{

buf.append( jg[i] ).append(":" );

buf.append( jig[j] ).append(":" );

buf.append( Date() );

result[k] = buf.toString();

buf =null;

}

}

}

}

I want to add to result string array by assigning from buf which is StringBuffer in a loop. But what happens after buf = null?

Will the added String be gone?

Or should I just do:

buf = "";

and continue with the loop and the string objects will be preserved? But isn't result just an array of references?

Many thanks,

[1796 byte] By [navrsalea] at [2007-10-2 6:04:48]
# 1

for( j=0; i < jig.length; j++, k++ )

{

buf.append( jg[i] ).append( ":" );

buf.append( jig[j] ).append( ":" );

buf.append( Date() );

result[k] = buf.toString();

buf = new StringBuffer();

}

however, result looks null to me so result[k] will throw NullPointerException unless i've missed something.

walken16a at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...
# 2
String[]tg;//?Date startTime;//?buf.append( Date() );//?
walken16a at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...
# 3

Not quite sure what you are trying to do, but a few mistakes do stand out

In your second loop, you have the condition i<jig.length.

Shouldn't that be j?

buf.append(Date())

Maybe you mean new Date()?

Maybe you want to format a date with a SimpleDateFormat?

- buf = null

If you want to clear the buffer try

buf.setLength(0)

or

buf = new StringBuffer();

You have already copied the value out of the string buffer into a String that is safely stored in the array. That String won't be lost.

Strings are immutable, so once they have a value, they don't change.

Which is the reason that StringBuffers exist - so you have a String you can modify.>

evnafetsa at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...
# 4

I am not sure I understand correctly. Here is a test program (in real program I call APIs from a library which returns String[] for jg and for jig, i.e they are changing in the loop in runtime.

I tried to put together test program:

import java.util.*;

public class Test {

public void Test() {

}

public String[] getSJ()

{

String[] jg = {"g1", "g2"};

String[] jig = {"1", "2", "3"};

String[]result = new String[50];

StringBuffer buf = new StringBuffer();

int i;

int j;

int k = 0;

for( i=0; i < jg.length; i++ )

{

for( j=0; i < jig.length; j++, k++ )

{

buf.append( jg[i] )

.append( ":" )

.append( jig[j] )

.append( ":" )

.append( new Date() );

result[k] = buf.toString();

buf = null;

}

}

return result;

}

public static void main( String[] args ) {

Testt = new Test();

String[] res = t.getSJ();

for( int i=0; i<res.length; i++ ) {

System.out.println( res[i] );

}

}

}

but when I run it fails in this statement:

buf.append( jg[i] )

.append( ":" )

.append( jig[j] )

.append( ":" )

.append( new Date() );

Exception in thread main

java.lang.NullPointerException

java.lang.String[] Test.getSJ()

Test.java:24

void Test.main(java.lang.String[])

Test.java:38

Wierd!?

And if I change to:

buf.setLength(0);

then it fails with this error:

Exception in thread main

java.lang.ArrayIndexOutOfBoundsException: 3

java.lang.String[] Test.getSJ()

Test.java:24

void Test.main(java.lang.String[])

Test.java:38

Also, I would like not to have to allocate like in:

String[]result = new String[50];

because I do not know how many different Strings in array will be returned by API. Is it possible to dynamically adjust

String[] result

somehow?

Many thanks,>

navrsalea at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...
# 5

I just saw evnafets remark regarding loop variable! Now it works, but I'd still like to know if it is possible to dynamically adjust result and not preallocate the size:

g1:1:Fri Nov 25 00:05:44 EST 2005

g1:2:Fri Nov 25 00:05:44 EST 2005

g1:3:Fri Nov 25 00:05:44 EST 2005

g2:1:Fri Nov 25 00:05:44 EST 2005

g2:2:Fri Nov 25 00:05:44 EST 2005

g2:3:Fri Nov 25 00:05:44 EST 2005

null

null

null

null

null

null

Thanks!

navrsalea at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...
# 6

in this loop:

for( j=0; i < jig.length; j++, k++ ) {

buf.append( jg[i] )

.append( ":" )

.append( jig[j] )

.append( ":" )

.append( new Date() );

result[k] = buf.toString();

buf = null;

}

you set buf to null at the end of the first iteration. It will throw a NPE

on the second iteration when you try to call append.

walken16a at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...
# 7
> I'd still like to know if it is> possible to dynamically adjust result and not> preallocate the size:You could use one of these[url] http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html[/url]
walken16a at 2007-7-16 13:05:18 > top of Java-index,Java Essentials,New To Java...