a simple question realted to StringBuffer

Discussion in 'Java' started by Shawn, Dec 13, 2006.

  1. Shawn

    Shawn Guest

    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.
     
    Shawn, Dec 13, 2006
    #1
    1. Advertising

  2. 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
     
    Flo 'Irian' Schaetz, Dec 13, 2006
    #2
    1. Advertising

  3. 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.
     
    Andrew Thompson, Dec 13, 2006
    #3
  4. Shawn

    Shawn Guest

    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.
     
    Shawn, Dec 13, 2006
    #4
  5. 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
     
    Flo 'Irian' Schaetz, Dec 13, 2006
    #5
  6. Shawn

    Shawn Guest

    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.
     
    Shawn, Dec 13, 2006
    #6
  7. Shawn

    Shawn Guest

    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.
     
    Shawn, Dec 13, 2006
    #7
  8. 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
     
    Flo 'Irian' Schaetz, Dec 13, 2006
    #8
  9. 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
     
    Flo 'Irian' Schaetz, Dec 13, 2006
    #9
  10. And thus, Flo 'Irian' Schaetz (that's me) spoke...

    ....something.

    P.S. Your code works fine here.

    Flo
     
    Flo 'Irian' Schaetz, Dec 13, 2006
    #10
  11. Shawn

    Shawn Guest

    Flo 'Irian' Schaetz wrote:
    > 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.
     
    Shawn, Dec 13, 2006
    #11
  12. Shawn

    Eric Sosman Guest

    Shawn wrote:
    > 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.


    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.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 13, 2006
    #12
  13. Shawn

    Daniel Pitts Guest

    Shawn wrote:
    > 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.


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

    array[5] = 'A';
    array[10] = 'B';
    System.out.println(new String(array));
     
    Daniel Pitts, Dec 13, 2006
    #13
  14. Shawn

    josh Guest

    Shawn napisał(a):
    > Flo 'Irian' Schaetz wrote:
    >> 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.


    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.
     
    josh, Dec 13, 2006
    #14
  15. Shawn

    Ian Wilson Guest

    Shawn wrote:
    > 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.


    <snip>

    > 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 :)
     
    Ian Wilson, Dec 14, 2006
    #15
  16. Shawn

    Shawn Guest

    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.
     
    Shawn, Dec 14, 2006
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    3
    Views:
    1,869
  2. qazmlp
    Replies:
    5
    Views:
    745
    Chris Smith
    Feb 19, 2004
  3. John Galt

    hash for String and StringBuffer

    John Galt, Feb 24, 2004, in forum: Java
    Replies:
    4
    Views:
    568
    Doug Pardee
    Feb 25, 2004
  4. Replies:
    16
    Views:
    579
  5. question realted to void *

    , Sep 16, 2008, in forum: C Programming
    Replies:
    22
    Views:
    722
    Tim Rentsch
    Oct 9, 2008
Loading...

Share This Page