2013-11-24 18 views
0

我编写了一个代码,用于搜索HTML代码并查找其中的链接。 HTML代码中的行有一些不必要的字符,所以我需要删除开始和结束。这是一条线的HTML代码示例:提取字符串中间错误

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

我的代码贴在下面,这工作完全正常,直到我添加字符串bestUrl,在这种情况下,它给我的错误:

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at Java.lang.string.substring(String.java:1904)
at 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()); 
    } 

} 
} 

任何想法,将不胜感激,谢谢你了!

+0

我在薄主逻辑{字符串bestUrl = words.substring(的startIndex,finishIndex);},则需要检查startIndex和finishIndex是否不等于-1。 –

+0

这些正则表达式错误'String beginIndex =“h:\”..“; String endIndex =”\“,c:”;'。他们没有找到,这使得'word.indexOf()= -1;' –

+0

你能告诉我们一些样本输入和期望的输出?,并且一些边缘情况会很好。 – Bohemian

回答

0

由于您在设置inSongs = true后忘记阅读下一行,因此您的数组超出了范围。我在打印出歌曲列表的代码块中添加了一个额外的readline以及一个空检查。当我使用eddievedder作为main的输入参数时,修改后的代码完美运行。

改进型Code下面

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()); 
     } 

    } 
}