提取字符串中间时出错

时间:2013-11-24 03:27:49

标签: java string substring extract

我编写的代码搜索HTML代码并查找其中的链接。 HTML代码中的行有一些不必要的字符,所以我需要删除开头和结尾。这是HTML代码行的示例:

{s:"Hate Being Sober", h:"../lyrics/chiefkeef/hatebeingsober.html", c:"", a:""}

我的代码发布在下面,它完全正常,直到我添加String bestUrl,在这种情况下它给了我错误:

  

“线程中的异常”主“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1
  在Java.lang.string.substring(String.java:1904)
  在CussCount.main(CussCount.java:32)

这是我的代码:

import java.io.*;
import java.net.*;
public class CussCount{
public static void main(String args[]){
    try{
        String artist=args[0];
        String first=artist.substring(0,1);
        Boolean inSongs=false;
        String beginIndex= "h:\"..";
        String endIndex="\", c:";
        int one=1;
        URL discography = new URL("http://www.azlyrics.com/"+first+"/"+artist+".html");
        URLConnection xx = discography.openConnection();
        BufferedReader xy = new BufferedReader(new InputStreamReader(
                    xx.getInputStream()));
        String words = xy.readLine();
        while(words!=null){
            if(words.equals("var songlist = [")){
                inSongs=true;
            }
            if(words.equals("var res = '<br />';")){
                inSongs=false;
                break;
            }
            if(inSongs==true){
                System.out.println(words);
                int startIndex= words.indexOf(beginIndex,one);
                System.out.println(startIndex+6);
                int finishIndex= words.indexOf(endIndex,one);
                System.out.println(finishIndex);

                String bestUrl=words.substring(startIndex, finishIndex);
                System.out.println(bestUrl);
            }

            words = xy.readLine();
        }
        xy.close();
    }catch(IOException ioe){
        System.out.println(ioe.getMessage());
    }

}
}

非常感谢任何想法,非常感谢!!

1 个答案:

答案 0 :(得分:0)

你的数组越界越界,因为你设置 inSongs = true 后忘了阅读下一行。我在代码块中添加了一个额外的readline和一个空检查,打印出歌曲列表。当我使用 eddievedder 作为main的输入参数时,修改后的代码运行得很好。

修改后的代码

import java.io.*;
import java.net.*;
public class CussCount{
    public static void main(String args[]){
        try{
            String artist=args[0];
            String first=artist.substring(0,1);
            Boolean inSongs=false;
            String beginIndex= "h:\"..";
            String endIndex="\", c:";
            int one=1;
            URL discography = new URL("http://www.azlyrics.com/"+first+"/"+artist+".html");
            URLConnection xx = discography.openConnection();
            BufferedReader xy = new BufferedReader(new InputStreamReader(
                    xx.getInputStream()));
            String words = xy.readLine();
            while(words!=null){
                if(words.equals("var songlist = [")){
                    inSongs=true;
                    words = xy.readLine();
                }
                if(words.equals("var res = '<br />';")){
                    inSongs=false;
                    break;
                }
                if(inSongs==true && words!=null){
                    System.out.println(words);
                    int startIndex= words.indexOf(beginIndex,one);
                    System.out.println(startIndex+6);
                    int finishIndex= words.indexOf(endIndex,one);
                    System.out.println(finishIndex);

                    String bestUrl=words.substring(startIndex, finishIndex);
                    System.out.println(bestUrl);
                }

                words = xy.readLine();
            }
            xy.close();
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }

    }
}