Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > a simple question realted to StringBuffer

Reply
Thread Tools

a simple question realted to StringBuffer

 
 
Shawn
Guest
Posts: n/a
 
      12-13-2006
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.
 
Reply With Quote
 
 
 
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      12-13-2006
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-13-2006
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.

 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      12-13-2006
Flo 'Irian' Schaetz wrote:

> 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.


 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      12-13-2006
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
 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      12-13-2006
Andrew Thompson wrote:
> 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.
>


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.
 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      12-13-2006
Flo 'Irian' Schaetz wrote:

>
> 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.
 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      12-13-2006
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
 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      12-13-2006
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
 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      12-13-2006
And thus, Flo 'Irian' Schaetz (that's me) spoke...

....something.

P.S. Your code works fine here.

Flo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question realted to void * junky_fellow@yahoo.co.in C Programming 22 10-09-2008 06:38 AM
Noob question - StringBuffer fxtrad@gmail.com Java 16 01-09-2008 06:01 AM
New Forum about Mp3 players and realted topics Rick Computer Support 1 04-17-2004 08:12 PM
buff = new StringBuffer(10000); buff.toString(); Will GC be complete here? qazmlp Java 5 02-19-2004 04:07 PM
performance string, stringbuffer, and stringtoken Mike mmccaws@netscape.net Java 3 07-07-2003 02:51 PM



Advertisments