0

I have a String value which has a maximum length of 629 characters. i am using StringBuffer to insert values on specific offset index.

     StringBuffer sb = new StringBuffer("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           ");
 sb.insert(0, "IN1");

 sb.insert(23, "abcsdfsfdsffdsffd");
 sb.insert(70, "6001");
 sb.insert(75, "74");
 sb.insert(80, "arn:organization");
 sb.insert(128, "YYYYMMDDHHMMSS");
 sb.insert(142, "0");
 sb.insert(145, "arn:organization");
 sb.insert(169, "502");
 sb.insert(193, "1");
 sb.insert(223, "1");
 sb.insert(228, "6001");
 sb.insert(236, "14228");
 sb.insert(254, "1");
 sb.insert(259, "4.334");
 sb.insert(514, "Usage");
 sb.insert(594, "0");

if you can see from the sample codes, i will have to initialize the StringBuffer with literally 629 blank space... else the insert will not work.

i tried StringBuffer sb = new StringBuffer(629);

but when i tried to insert into index 23, it throws an error of index out of bounds.

is there a more elegant way to initialize the StringBuffer to insert string on index?

KeanHo Leong
  • 67
  • 12
  • I guess you should choose some data structure like `MAP` and then append the value into string – Ryuzaki L Jun 24 '20 at 12:46
  • In what line it throws the error ? – Dimitri Jun 24 '20 at 12:50
  • 1
    What exactly are you trying to do? Your description says "has a maximum length" but you appear to be trying to have exactly that length. – asm Jun 24 '20 at 12:50
  • sb.insert(23, "abcsdfsfdsffdsffd"); this one throws an error. – KeanHo Leong Jun 24 '20 at 12:51
  • Better to use ```append``` method here. And the places wherever you want spaces just mention in the ```append``` method itself. – Nitin Singhal Jun 24 '20 at 12:52
  • a MAP wouldn't help because i want different values to start at different index of the string. – KeanHo Leong Jun 24 '20 at 12:53
  • @NitinSinghal. but append doesn't allow me start specify the starting index of where the value needs to appear. the output has to be padded in the correct index for the different values. – KeanHo Leong Jun 24 '20 at 12:54
  • @joe the linked question for the Duplicate close reason doesn't look relevant to me. It is https://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java at the moment. The answer to this question is that StringBuffer needs to be expanded as needed with null or space or other characters. Presumably after it's large enough, offsets within the existing string buffer can be modified. – Kind Contributor Jun 28 '20 at 14:35

1 Answers1

1

You are initializing the StringBuffer incorrectly. The StringBuffer(int) constructor does not create a string with the given length, it merely allocates capacity to handle it. The StringBuffer will still have a length of 0 at the beginning. This is causing your error.

You need to initialize it with either the StringBuffer(CharSequence) or StringBuffer(String) constructor.

Create a string with length 629 using any of the methods outlined in this answer and use that to initialize your StringBuffer.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60