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?

8
  • I guess you should choose some data structure like MAP and then append the value into string Commented Jun 24, 2020 at 12:46
  • In what line it throws the error ? Commented Jun 24, 2020 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. Commented Jun 24, 2020 at 12:50
  • sb.insert(23, "abcsdfsfdsffdsffd"); this one throws an error. Commented Jun 24, 2020 at 12:51
  • Better to use append method here. And the places wherever you want spaces just mention in the append method itself. Commented Jun 24, 2020 at 12:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.