| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 391 人关注过本帖
标题:查找字符串,编译通过了,可是运行不行,大家帮着找找哪里需要改动
只看楼主 加入收藏
tottibuffon
Rank: 2
等 级:论坛游民
帖 子:105
专家分:20
注 册:2009-6-5
结帖率:83.87%
收藏
已结贴  问题点数:20 回复次数:2 
查找字符串,编译通过了,可是运行不行,大家帮着找找哪里需要改动
public class FindString
{
    public static void main(String[] args)
    {
        String string1="I don't wanna say goodbye to you";
        int len=string1.length();
        int j=17;
        int i=9;
        int k=0;
        char []a=new char[len];
        
        a[0]=string1.charAt(i);
        
        
        for( ; i<=j ; i++){
            for(;;k++){
              a[k]=string1.charAt(i);
            }
        }
        String string2=new String(a);
        System.out.println("the char at "+i+" is:"+a[0]);//字符查找
        System.out.println("the string from"+i+"to"+j+"is:");//串查找
        System.out.println(string2);


    }
}


--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at FindString.main(FindString.java:17)

Process completed.
搜索更多相关主题的帖子: 编译 运行 字符 
2009-10-11 09:42
tottibuffon
Rank: 2
等 级:论坛游民
帖 子:105
专家分:20
注 册:2009-6-5
收藏
得分:0 
补充下,经过我的修改变成这样,但是获取子串的结果还是有问题
public class FindString
{
    public static void main(String[] args)
    {
        String string1="I don't wanna say goodbye to you";
        int len=string1.length();
        int j=17;
        int i=9;
        char []a=new char[len];
        
        a[0]=string1.charAt(i);
        
        String string2=new String(a);
        String string3=string2.substring(i,j);
        
        System.out.println("the char at "+i+" is:"+a[0]);//获取字符
        System.out.println("the string from"+i+"to"+j+"is:");//获取子串
        System.out.println(string3);


    }
}



--------------------Configuration: <Default>--------------------
the char at 9 is:a
the string from9to17is:

Process completed.

2009-10-11 09:55
windizual
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:124
专家分:186
注 册:2009-7-1
收藏
得分:0 
~~~~楼主多看看API文档吧,String下已经有现成的方法调用的了

Java要学的东西好多~~~~~
2009-10-12 01:06
快速回复:查找字符串,编译通过了,可是运行不行,大家帮着找找哪里需要改动
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020321 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved