| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 746 人关注过本帖
标题:一个小问题,,,麻烦看看,,
只看楼主 加入收藏
yyow3193
Rank: 1
来 自:三峡宜昌
等 级:新手上路
帖 子:165
专家分:0
注 册:2008-6-5
收藏
 问题点数:0 回复次数:2 
一个小问题,,,麻烦看看,,
public class demo
{
    public static void main(String[] args)
    {
        String text="fang xing is no one";
        int count=0;
        int index=0;
        do
        {
            ++count;
            ++index;
            index=text.indexOf(' ',index);
        }
        while(index !=-1);
        System.out.println(index);
        System.out.println(count);
        index=0;
        int endindex=0;
        String[] array=new String[count];
        for(int i=0;i<count;i++)
        {
            endindex=text.indexOf(' ',index);
            if(index==-1)
            array[i]=text.substring(index);
            else
            if(index !=-1)
            array[i]=text.substring(index,endindex);
            index=endindex+1;
            System.out.println(array[i]);
        }
    //    for(int i=0;i<5;i++)
    //    System.out.println(array[i]);
    }
}
怎么不能输出:
fang
xing
is
no
one
少一个one
运行后提示:
-1
5
fang
xing
is
no
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: -17
        at java.lang.String.substring(String.java:1768)
        at demo.main(demo.java:27)
Press any key to continue...
搜索更多相关主题的帖子: 麻烦 
2008-10-19 20:52
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:0 
public class demo
{
    public static void main(String[] args)
    {
        String text="fang xing is no one";
        String[] array = text.split(" ");
        for(String str:array){
            System.out.println(str);
        }
    }
}



这个?
2008-10-19 21:15
yyow3193
Rank: 1
来 自:三峡宜昌
等 级:新手上路
帖 子:165
专家分:0
注 册:2008-6-5
收藏
得分:0 
哈,,,,我知道了,,原来是
endindex=text.indexOf(' ',index);
                if(endindex==-1)
                array[i]=text.substring(index);这里的问题,,,
2008-10-19 21:34
快速回复:一个小问题,,,麻烦看看,,
数据加载中...
 
   



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

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