获取错误“字符串索引超出范围:0”使用String Builder [英] Getting error "String index out of range: 0" on using String Builder

查看:573
本文介绍了获取错误“字符串索引超出范围:0”使用String Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我在Java中的代码(用于返回字符串的两半 - 一个是奇数半部分,从索引0开始,或者第一个字符,后半部分,从索引1开始,或者第二个字符字符):

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);
     }
}

所以我的问题是 - 每当我试图运行这个在BlueJ IDE上的程序,它没有,并在评论中提到的行上返回标题中的错误。

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.

我已倾倒此网站以获得类似问题可以帮助我解决我的错误,但我发现的所有问题都表明我已经实现了一个改变(在StringBuilder的setCharAt方法中,该人已经颠倒了i和ch参数)。

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).

这与输出最初被声明为空的事实有什么关系,而setCharAt方法只能替换已经存在的字符?
任何帮助都将不胜感激。

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.

推荐答案


这与它有什么关系事实上,output首先被声明为空,而setCharAt方法只能替换已经存在的字符?

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.

注意创建一个具有指定长度的 StringBuilder ,就像你一样做:

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

StringBuilder output = new StringBuilder(length);

创建 StringBuilder 已经有那么多字符 - 它只是用内部缓冲区大小创建一个 StringBuilder StringBuilder 本身仍然不包含任何字符,因此尝试设置第一个字符将导致您获得的异常。

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.

StringBuilder 上调用追加添加字符而不是 setCharAt

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

这篇关于获取错误“字符串索引超出范围:0”使用String Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆