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

Siddhartha Khanooja

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.

Jesper

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Gradle Error: String index out of range: 0

From Dev

String index out of range: 0

From Dev

charAt(0) String index out of range: 0

From Dev

StringIndexOutOfBoundsException: String index out of range: 0 while reading a file

From Dev

Java getting error while executing Adb Commands using process builder

From Dev

android: getting an error in using spannable string in android

From Dev

Adding RadEditor using javascrpt:getting unterminated string literal error

From Dev

shellscript in C: using awk getting runaway string constant error

From Dev

Getting an error using mysqli_escape_string function

From Dev

Ownership error using builder pattern

From Dev

Error using CurrentDb.Execute(sqlStr) despite SQL string functioning properly in query builder

From Dev

Getting an error using ScrimInsetFrameLayout

From Dev

Getting error in using TabHost

From Dev

Using Generic class with type String and getting error 'string' must be non-abstract type

From Dev

Getting error of Input String Format

From Dev

Laravel Query Builder - Error for using NOW() and DateFormat

From Dev

Laravel Query Builder - Error for using NOW() and DateFormat

From Dev

String builder not using string literal from string pool. Why?

From Dev

Getting data from three tables codeigniter using Query builder

From Dev

Getting fresh result set using Laravel's Query Builder

From Dev

getting the error: String reference not set to an instance of a String

From Dev

Getting part of string using javascript

From Dev

Getting the longest string using properties

From Dev

Getting error while using pip

From Dev

Getting this error when using tweepy

From Dev

Using generics and polymorphism, getting error

From Dev

Using Nsubstitute for mocking but getting an error

From Dev

Using stargazer for lfe and getting error

Related Related

  1. 1

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

  2. 2

    Gradle Error: String index out of range: 0

  3. 3

    String index out of range: 0

  4. 4

    charAt(0) String index out of range: 0

  5. 5

    StringIndexOutOfBoundsException: String index out of range: 0 while reading a file

  6. 6

    Java getting error while executing Adb Commands using process builder

  7. 7

    android: getting an error in using spannable string in android

  8. 8

    Adding RadEditor using javascrpt:getting unterminated string literal error

  9. 9

    shellscript in C: using awk getting runaway string constant error

  10. 10

    Getting an error using mysqli_escape_string function

  11. 11

    Ownership error using builder pattern

  12. 12

    Error using CurrentDb.Execute(sqlStr) despite SQL string functioning properly in query builder

  13. 13

    Getting an error using ScrimInsetFrameLayout

  14. 14

    Getting error in using TabHost

  15. 15

    Using Generic class with type String and getting error 'string' must be non-abstract type

  16. 16

    Getting error of Input String Format

  17. 17

    Laravel Query Builder - Error for using NOW() and DateFormat

  18. 18

    Laravel Query Builder - Error for using NOW() and DateFormat

  19. 19

    String builder not using string literal from string pool. Why?

  20. 20

    Getting data from three tables codeigniter using Query builder

  21. 21

    Getting fresh result set using Laravel's Query Builder

  22. 22

    getting the error: String reference not set to an instance of a String

  23. 23

    Getting part of string using javascript

  24. 24

    Getting the longest string using properties

  25. 25

    Getting error while using pip

  26. 26

    Getting this error when using tweepy

  27. 27

    Using generics and polymorphism, getting error

  28. 28

    Using Nsubstitute for mocking but getting an error

  29. 29

    Using stargazer for lfe and getting error

HotTag

Archive