Siddhartha Khanooja
Siddhartha Khanooja

Reputation: 67

Getting error "String index out of range: 0" on using String Builder

So this is my code in Java (for returning two halves of a string - one is the odd half, starting with the index 0, or the first character, and the second half, starting with the index 1, or the second character):

public class StringTest{
     public String halfOfString(String message, int start){
            int length = message.length();
            StringBuilder output = new StringBuilder(length); 
            char ch;
            int i;
            if((start==0)||(start==1)){
               for(i=start; i<message.length(); i=i+2){
                            ch = message.charAt(i);
                            output.setCharAt(i,ch); // error occurs here
               }

            }
     return output.toString();
     }

     public void testFunction(){
           String s = "My name is Sid";
           String first = halfOfString(s, 0);
           String second = halfOfString(s, 1);
           System.out.println("First half is " + first + " and second half is " + second);
     }
}

So my problem is - whenever I attempt to run this program on BlueJ IDE, it doesn't, and returns the error in the title, on the line mentioned in the comment.

I have poured over this site for a similar question which may help me with my error, but all I found was a question which suggested a change I have already implemented (in the StringBuilder setCharAt method, the person had reversed the i and ch parameters).

Is it anything to do with the fact that the "output" is declared empty at first, and the setCharAt method can only replace the characters which already exist? Any help would be greatly appreciated.

Upvotes: 1

Views: 3337

Answers (2)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

Is it anything to do with the fact that the "output" is declared empty at first, and the setCharAt method can only replace the characters which already exist?

Yes, that's the reason.

The Javadoc of setCharAt is clear on this:

The index argument must be greater than or equal to 0, and less than the length of this sequence.

Why don't you just use the append method in StringBuilder? It looks to me like, if your code would work, it would leave holes in the StringBuilder since you're only setting every other character and not the ones in between. Using append ensures that there are no holes.

Upvotes: 5

Jesper
Jesper

Reputation: 206776

Is it anything to do with the fact that the "output" is declared empty at first, and the setCharAt method can only replace the characters which already exist?

Yes, that is exactly why you get this error.

Note that creating a StringBuilder with a specified length, as you are doing:

StringBuilder output = new StringBuilder(length);

does not create a StringBuilder which already has that many characters - it just creates a StringBuilder with that internal buffer size. The StringBuilder itself still contains no characters, so trying to set the first character will result in the exception that you get.

Call append on the StringBuilder to add characters instead of setCharAt.

Upvotes: 9

Related Questions