a simple question realted to StringBuffer

S

Shawn

Hi,

I am using a StringBuffer to hold a line with fixed length of 72 chars.
Later, I am going to put char at a specified location, using
setCharAt(location, char) method.

But this code doesn't work:
<Java>
StringBuffer line = new StringBuffer(72);

line.setCharAt(5, 'A');
</Java>

This code works:
<Java>
StringBuffer line = new StringBuffer(72);
for (int i=0; i<72; i++) //make the StringBuffer contains 72 empty chars
{
line.append(' ');
}

line.setCharAt(5, 'A');
</Java>

I found that this required for loop is ridiculous. Reading the Java
document of StringBuffer suggests the first version is correct and
should work. I don't understand it.

Thank you very much for your help.
 
F

Flo 'Irian' Schaetz

And thus, Shawn spoke...
But this code doesn't work:
<Java>
StringBuffer line = new StringBuffer(72);

line.setCharAt(5, 'A');
</Java> ....
I found that this required for loop is ridiculous. Reading the Java
document of StringBuffer suggests the first version is correct and
should work. I don't understand it.

The (new) StringBuffer has the length() zero. So setting the char at 5
isn't possible. 72 isn't the size of the string, it's just the size of
the array behind it.

The doc says to setCharAt(...):

IndexOutOfBoundsException - if index is negative or greater than or
equal to length().

Read also the documentation of length() and the constructors. They don't
set a length, just the initial capacity. This capacity is important - if
you choose it to small, the StringBuffer will have to increase it's
internal size. If you choose it to big, you will waste memory. But the
length will only increase if you append something to it.

Flo
 
A

Andrew Thompson

Shawn wrote:
....

What Flo said +..
I am using a StringBuffer to hold a line with fixed length of 72 chars.

But if it is specifically and always 72 chars, why
not make it a char array? You can set any index
to any char you want, and leave array elements
before and after it, untouched (null).

Andrew T.
 
S

Shawn

Flo said:
The (new) StringBuffer has the length() zero. So setting the char at 5
isn't possible. 72 isn't the size of the string, it's just the size of
the array behind it.

The doc says to setCharAt(...):

IndexOutOfBoundsException - if index is negative or greater than or
equal to length().

Read also the documentation of length() and the constructors. They don't
set a length, just the initial capacity. This capacity is important - if
you choose it to small, the StringBuffer will have to increase it's
internal size. If you choose it to big, you will waste memory. But the
length will only increase if you append something to it.

Flo
Thank you for your help. It is very helpful for me. But now, I found
something really bizarre. And I hope that this time I have not missed
any hints from the Java documentations:

<Java>
StringBuffer line = new StringBuffer(72); //I understand now, capacity
= 72, but length() = 0
line.setLength(72); //now both capacity and length is 72

line.setCharAt(5, 'A'); //No error!!! But:

System.out.println("line = " + line.toString()); // only prints "line =
", in another word, line.toString() is EMPTY!!!!!

System.out.println("Char at 5 = " + line.CharAt(5)); // prints out "Char
at 5 = A" !!!!!!!

</Java>

Is this amazing?! I have read toString() documents several times:

toString

public String toString()

Converts to a string representing the data in this string buffer. ....

I really don't understand it. Thank you for your help.
 
F

Flo 'Irian' Schaetz

And thus, Shawn spoke...

<Java>
StringBuffer line = new StringBuffer(72); //I understand now, capacity
= 72, but length() = 0
line.setLength(72); //now both capacity and length is 72

line.setCharAt(5, 'A'); //No error!!! But:

System.out.println("line = " + line.toString()); // only prints "line =
", in another word, line.toString() is EMPTY!!!!!

System.out.println("Char at 5 = " + line.CharAt(5)); // prints out "Char
at 5 = A" !!!!!!!

</Java>

Is this amazing?! I have read toString() documents several times:

There's something wrong with your example, but I don't see what...

StringBuffer sb = new StringBuffer(72);
sb.setLength(72);
sb.setCharAt(5, 'A');
System.out.println(sb.toString() + ".");

....works fine for me (1.5). It prints 4 "zero"-chars (squares), an 'A',
another 77 zero-chars and a '.'.

Somewhere your example must have a bug :)

Flo
 
S

Shawn

Andrew said:
Shawn wrote:
...

What Flo said +..


But if it is specifically and always 72 chars, why
not make it a char array? You can set any index
to any char you want, and leave array elements
before and after it, untouched (null).

Andrew T.

I have tried using char array. But I ran into another problem. I cannot
easily convert the char array into a String and print it out. I cannot
recall the exact details. But it seems very silly:

char[] array = new char[72];
array[5] = 'A';
array[10] = 'B';

Now if I want to print out this line, I expect there are 5 spaces in the
beginning, then 'A', then 4 spaces, then B. It didn't work out. It
actually is very painful to do it. I cannot make it work so I gave up.
 
S

Shawn

Flo said:
There's something wrong with your example, but I don't see what...

StringBuffer sb = new StringBuffer(72);
sb.setLength(72);
sb.setCharAt(5, 'A');
System.out.println(sb.toString() + ".");

...works fine for me (1.5). It prints 4 "zero"-chars (squares), an 'A',
another 77 zero-chars and a '.'.

Somewhere your example must have a bug :)

Flo

It is bizarre. Here is my code:
<Java>
public class Test
{
public static void main(String[] args)
{
StringBuffer line = new StringBuffer(72);
line.setLength(72);
line.setCharAt(5, 'A');
System.out.println("line = " + line.toString());
System.out.println("line = " + line.toString() +".");
System.out.println("Char at 5 = " + line.charAt(5));
}
}
</Java>

Here is the output:
line =
line =
Char at 5 = A

I am using Eclipse Java 1.5.
 
F

Flo 'Irian' Schaetz

And thus, Shawn spoke...
char[] array = new char[72];
array[5] = 'A';
array[10] = 'B';

Simple:

String s = new String(array);

But be aware... A char is 0 by default - and this is NOT the same as a
space... It's normaly displayed as a square. So you will have 70
zero-chars and an A and B. But the 70 zero-chars are NOT spaces.

Flo
 
F

Flo 'Irian' Schaetz

And thus, Shawn spoke...

Did you save your file? :) Because...
System.out.println("line = " + line.toString());
System.out.println("line = " + line.toString() +".");
System.out.println("Char at 5 = " + line.charAt(5));
and...

Here is the output:
line =
line =
Char at 5 = A

....doesn't match. Do you see the missing "." at the end of the 2nd "line
=" output?

Flo
 
F

Flo 'Irian' Schaetz

And thus, Flo 'Irian' Schaetz (that's me) spoke...

....something.

P.S. Your code works fine here.

Flo
 
S

Shawn

Flo said:
And thus, Flo 'Irian' Schaetz (that's me) spoke...

...something.

P.S. Your code works fine here.

Flo

OK. I think I gave up. Just use the silly for loop appending ' ' 72
times, like I mentioned in the first posting.

My code does not work here. We are using linux. It is bizarre that Java
does not run everywhere.

Thank you for your help. I greatly appreciate it.
 
E

Eric Sosman

Shawn said:
Flo said:
There's something wrong with your example, but I don't see what...

StringBuffer sb = new StringBuffer(72);
sb.setLength(72);
sb.setCharAt(5, 'A');
System.out.println(sb.toString() + ".");

...works fine for me (1.5). It prints 4 "zero"-chars (squares), an
'A', another 77 zero-chars and a '.'.

Somewhere your example must have a bug :)

Flo

It is bizarre. Here is my code:
<Java>
public class Test
{
public static void main(String[] args)
{
StringBuffer line = new StringBuffer(72);
line.setLength(72);
line.setCharAt(5, 'A');
System.out.println("line = " + line.toString());
System.out.println("line = " + line.toString() +".");
System.out.println("Char at 5 = " + line.charAt(5));
}
}
</Java>

Here is the output:
line =
line =
Char at 5 = A

I am using Eclipse Java 1.5.

A hunch: Do you get the same result when you run the
program stand-alone, without Eclipse?

java your.package.name.here.Test

It is possible that Eclipse is doing something peculiar
with output lines containing '\u0000' characters -- for
example, treating the '\u0000' as a string terminator, a la C.
 
D

Daniel Pitts

Shawn said:
Andrew said:
Shawn wrote:
...

What Flo said +..


But if it is specifically and always 72 chars, why
not make it a char array? You can set any index
to any char you want, and leave array elements
before and after it, untouched (null).

Andrew T.

I have tried using char array. But I ran into another problem. I cannot
easily convert the char array into a String and print it out. I cannot
recall the exact details. But it seems very silly:

char[] array = new char[72];
array[5] = 'A';
array[10] = 'B';

Now if I want to print out this line, I expect there are 5 spaces in the
beginning, then 'A', then 4 spaces, then B. It didn't work out. It
actually is very painful to do it. I cannot make it work so I gave up.

char[] array = new char[72];
java.util.Arrays.fill(array, ' ');

array[5] = 'A';
array[10] = 'B';
System.out.println(new String(array));
 
J

josh

Shawn napisał(a):
OK. I think I gave up. Just use the silly for loop appending ' ' 72
times, like I mentioned in the first posting.

My code does not work here. We are using linux. It is bizarre that Java
does not run everywhere.

I think the problem is connected with what Flo 'Irian' Schaetz said in
this thread before (that "0" chars are not spaces). I think the reason
why the same piece of code works for Flo 'Irian' Schaetz and does not
work for Shawn. You are streaming your output into different console
implementations. One console accepts "zero" char and displays it as
'space', another console might igonore any character following 'zero char'.

Look exactlt at that example:
<Java>
public class Test
{
public static void main(String[] args)
{
StringBuffer line = new StringBuffer(72);
line.setLength(72);
line.setCharAt(5, 'A');
System.out.println("line = " + line.toString());
System.out.println("line = " + line.toString() +".");
System.out.println("Char at 5 = " + line.charAt(5));
}
}
</Java>

Here is the output:
line =
line =
Char at 5 = A

you don't see "." char at the end of secound line.
Once again: line.setLength(72) sets line's length but it DOES NOT put
'spaces' there. In one console 'zero char' can be displayed as 'space'
in another console/output view that string might be ignored.
 
I

Ian Wilson

Shawn said:
Hi,

I am using a StringBuffer to hold a line with fixed length of 72 chars.
Later, I am going to put char at a specified location, using
setCharAt(location, char) method.

I found that this required for loop is ridiculous.

How about
Stringbuffer line = new StringBuffer(" ");
except with 72 spaces?

Or
char[] c = new char[72];
StringBuffer line = new StringBuffer();
Arrays.fill(c, ' ');
line.append(c);
It rather looks as if Andrew is right and your problem fits char[]
better than StringBuffer.

Since the same loop is going on in the background, maybe it doesn't
matter which of the above you use. Maybe you could hide the loop in ...
line = MyUtils.makeStringBuffer(' ', 72);

In Perl I'd just write $line=' 'x72 :)
 
S

Shawn

Thank you for all your help. I greatly appreciate it.

Regarding the code that works for Flo'Irian'Schaetz but not for me, one
possible reason maybe due to cygwin. Sorry I didn't mention it
yesterday. I realized it on my way back home.

I am using cygwin to connect from my PC(Windows XP) to downstairs Linux
machine. I am using Window Maker and running Eclipse inside the Window
Maker (So Eclipse is running on the Linux machine). Probably error
happened when it encounters 'zero char' or 'space char'. I could go
downstairs and run Eclipse directly without cygwin involved. But I doubt
that it is worthy the time for such a small thing.

Thank you again for all your generous help. I highly appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top