异常处理,获取StringIndexOutOfBoundsException: [英] Exception handling, getting StringIndexOutOfBoundsException:

查看:163
本文介绍了异常处理,获取StringIndexOutOfBoundsException:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String s2 =samples;从s2字符串变量值中删除s字符中的任何一个???



请解决此问题1 ...从字符串中删除字符时我得到了以下异常,



线程main中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:115



String string =strings;

char [] arr = string.toCharArray();

StringBuilder builder = new StringBuilder();

for(int i = 0; i< arr.length; i ++)> if(arr [i] =='s'){

System.out.println(builder.deleteCharAt(arr) [i]));

}



}

String s2="samples"; Delete either of s character from a value of s2 string variable???

pls resolve this 1...While deleting char from string I'm getting follolwing Exception,

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 115

String string = "strings";
char[] arr = string.toCharArray();
StringBuilder builder=new StringBuilder();
for (int i=0; i<arr.length;i++)>if(arr[i]=='s'){
System.out.println(builder.deleteCharAt(arr[i]));
}

}

推荐答案

字符串生成器中没有任何内容,您不会使用所需的字符串初始化字符串生成器。您需要执行以下操作:



The string builder doesn't have anything in it, you don't initialize the string builder with the string you want. You need to do something like this:

StringBuilder builder = new StringBuilder(string);





否则构建器是一个空字符串,所以当你试图删除它时,没有任何东西,它会引发异常。抱歉如果上面没有编译,我不是Java程序员。



Otherwise builder is an empty string, so when you try to delete out of it, there is nothing there and it throws an exception. Apologies if the above does not compile, I'm not a Java programmer.


String string =strings;;

char [] arr = string。 toCharArray();

StringBuilder builder = new StringBuilder(string);

for(int i = 0,j = 0; j< arr.length; i ++,j ++) {

如果(arr [j] =='s'){

试试{

//System.out.println(removeCharAt( string,i));

System.out.println(builder.deleteCharAt(i));

i - ;

}

catch(StringIndexOutOfBoundsException siobe){

System.out.println(无效输入);

}





}



}
String string ="strings";;
char[] arr = string.toCharArray();
StringBuilder builder=new StringBuilder(string);
for (int i=0,j=0; j<arr.length;i++,j++) {
if(arr[j]=='s'){
try{
//System.out.println(removeCharAt(string, i));
System.out.println(builder.deleteCharAt(i));
i--;
}
catch(StringIndexOutOfBoundsException siobe){
System.out.println("invalid input");
}


}

}


这篇关于异常处理,获取StringIndexOutOfBoundsException:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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