关于substring(begin,end)的问题

Rick____ 2011-10-20 05:46:17
import java.util.Scanner;
public class homework3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入一段字符:");
String words=sc.next();
System.out.println("输入要查找的字符串:");
String toBeFound=sc.next();
int[] index=new int[10]; //存放下标位置的数组index
int count=0; //index数组元素下标计数
if(toBeFound.length()==1){
for(int i=0;i<words.length();i++){ //如果输入的是字符
if(words.substring(i,i+1).equals(toBeFound)){
index[count]=i;
count++;
}
}
}else if(toBeFound.length()>1){
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){//26行错误
index[count]=i;
count++;
}
}
}
System.out.println("“"+toBeFound+"”"+"出现的位置是:");
for(int i=0;i<index.length;i++){
if(index[i]!=999999999){ //未赋新值的index中的元素消去
System.out.print(index[i]+" ");
}
}
}
}


运行结果:输入一段字符:
wojioushiwo
输入要查找的字符串:
wo
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.lang.String.substring(String.java:1934)
at homework3.main(homework3.java:26)

java.lang.String.substring(String.java:1934)点进去看,拖黑的数据是:

if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}

各位帮小弟看下,是哪里出错了
...全文
499 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangqin12356 2011-10-20
  • 打赏
  • 举报
回复
words.substring(i,i+toBeFound.length()) ---这句的数组越界了,可以在之前做个判断,越界就跳出循环,代码修改如下:
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if(i+toBeFound.length()>words.length()){
break;
}
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){//26行错误
index[count]=i;
count++;
}
}
五哥 2011-10-20
  • 打赏
  • 举报
回复

import java.util.Scanner;
public class homework3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入一段字符:");
String words=sc.next();
System.out.println("输入要查找的字符串:");
String toBeFound=sc.next();
int[] index=new int[10]; //存放下标位置的数组index
int count=0; //index数组元素下标计数
if(toBeFound.length()==1){
for(int i=0;i<words.length();i++){ //如果输入的是字符
if (words.length() >= i +1 ){
if(words.substring(i,i+1).equals(toBeFound)){
index[count]=i;
count++;
}
}
}
}else if(toBeFound.length()>1){
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if (words.length() >= i+toBeFound.length() ){ //先判断 words是否有 i + toBeFound.length()这么长
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){
index[count]=i +1; //出现位置 从1开始
count++;
}
}
}
}
System.out.println("“"+toBeFound+"”"+"出现的位置是:");
for(int i=0;i<index.length;i++){
if(index[i]!=999999999){ //未赋新值的index中的元素消去
System.out.print(index[i]+" ");
}
}
}
}



zhangqin12356 2011-10-20
  • 打赏
  • 举报
回复
words.substring(i,i+toBeFound.length()) ---这句中的数组越界了;可以在之前加一个判断条件,如果越界就跳出循环。
代码修改如下:
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if(i+toBeFound.length()>words.length()){
break;
}
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){//26行错误
index[count]=i;
count++;
}
}
五哥 2011-10-20
  • 打赏
  • 举报
回复

import java.util.Scanner;
public class homework3 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("输入一段字符:");
String words=sc.next();
System.out.println("输入要查找的字符串:");
String toBeFound=sc.next();
int[] index=new int[10]; //存放下标位置的数组index
int count=0; //index数组元素下标计数
if(toBeFound.length()==1){
for(int i=0;i<words.length();i++){ //如果输入的是字符
if (words.length() >= i +1 ){
if(words.substring(i,i+1).equals(toBeFound)){
index[count]=i;
count++;
}
}
}
}else if(toBeFound.length()>1){
for(int i=0;i<words.length();i++){ //如果输入的字符串且长度>1
if (words.length() >= i+toBeFound.length() ){ //先判断 words是否有 i + toBeFound.length()这么长
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){
index[count]=i +1; //出现位置 从1开始
count++;
}
}
}
}
System.out.println("“"+toBeFound+"”"+"出现的位置是:");
for(int i=0;i<index.length;i++){
if(index[i]!=999999999){ //未赋新值的index中的元素消去
System.out.print(index[i]+" ");
}
}
}
}



五哥 2011-10-20
  • 打赏
  • 举报
回复
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){//26行错误


words.substring()之前 ,你应该判断一下 words.length >= i + toBeFound.length()

如果小于的话,当然越界啦 ,事实上你的就是报越界啦
五哥 2011-10-20
  • 打赏
  • 举报
回复
if(words.substring(i,i+toBeFound.length()).equals(toBeFound)){//26行错误


words.substring()之前 ,你应该判断一下 words.length >= i + toBeFound.length()

如果小于的话,当然越界啦 ,事实上你的就是报越界啦
liyy1986 2011-10-20
  • 打赏
  • 举报
回复
i+toBeFound.length()=12的时候报错的,你查查你字符有几位?
liyy1986 2011-10-20
  • 打赏
  • 举报
回复
i+toBeFound.length()=12了 应该报错了吧
liyy1986 2011-10-20
  • 打赏
  • 举报
回复
words.substring(i,i+toBeFound.length())这句话 查找的字符越界了吧,错误信息好像是说i+toBeFound.length()=12的时候查找出错吧
Rick____ 2011-10-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mengxiangyue 的回复:]
貌似应该有一个indexOf()函数 楼主试试这个函数
这个比较容易 原来写过类似的程序 我们加上了替换
[/Quote]

indexOf()只能差第一个吧,用循环如何历遍字符串找出所有需要搜索字符的位置?
孟祥月 2011-10-20
  • 打赏
  • 举报
回复
貌似应该有一个indexOf()函数 楼主试试这个函数
这个比较容易 原来写过类似的程序 我们加上了替换
Rick____ 2011-10-20
  • 打赏
  • 举报
回复
if(index[i]!=999999999){ //未赋新值的index中的元素消去
}
这个可以忽略(乱写的,不影响输出结果)

坐等楼下帮小弟解惑!
Rick____ 2011-10-20
  • 打赏
  • 举报
回复

if(index[i]!=999999999){ //未赋新值的index中的元素消去

}

这个条件可以忽略(乱写的忘记删了),不影响输出结果,坐等楼下帮我解惑!
Rick____ 2011-10-20
  • 打赏
  • 举报
回复
if(index[i]!=999999999){ //未赋新值的index中的元素消去

}
这个条件忽略吧 我乱写的。。不影响输出- -

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧