Java 提取字符串中间部分时出错
我正在编写一个代码,搜索HTML代码并查找其中的链接。HTML代码中的行有一些不必要的字符,因此我需要删除开头和结尾。这是一行HTML代码的示例:Java 提取字符串中间部分时出错,java,string,substring,extract,Java,String,Substring,Extract,我正在编写一个代码,搜索HTML代码并查找其中的链接。HTML代码中的行有一些不必要的字符,因此我需要删除开头和结尾。这是一行HTML代码的示例: {s:“讨厌清醒”,h:“../lyris/chiefkeef/hatebeingsober.html”,c:,a:” 我的代码发布在下面,在我添加字符串bestUrl之前,它完全可以正常工作,在这种情况下,它会给出错误: 线程“main”java.lang.StringIndexOutOfBoundsException中的“异常”:字符串索引超出
{s:“讨厌清醒”,h:“../lyris/chiefkeef/hatebeingsober.html”,c:,a:”
我的代码发布在下面,在我添加字符串bestUrl之前,它完全可以正常工作,在这种情况下,它会给出错误:
线程“main”java.lang.StringIndexOutOfBoundsException中的“异常”:字符串索引超出范围:-1在Java.lang.string.substring(string.Java:1904)
在cuscount.main(cuscount.java:32) 这是我的代码:
import java.io.*;
导入java.net。*;
公共类计数{
公共静态void main(字符串参数[]){
试一试{
字符串艺术家=args[0];
字符串优先=艺术家子字符串(0,1);
布尔内森=假;
字符串beginIndex=“h:\”;
字符串endIndex=“\”,c:;
int-one=1;
URL目录=新URL(“http://www.azlyrics.com/“+first+”/“+artist+”.html”);
URLConnection xx=discography.openConnection();
BufferedReader xy=新的BufferedReader(新的InputStreamReader(
xx.getInputStream());
字符串字=xy.readLine();
while(单词!=null){
if(words.equals(“var songlist=[”)){
内生=真;
}
if(words.equals(“var res=”
“;”){
内生=假;
打破
}
如果(内项==真){
System.out.println(字);
int startIndex=words.indexOf(beginIndex,一);
系统输出打印项次(startIndex+6);
int finishIndex=words.indexOf(endIndex,一);
系统输出打印LN(finishIndex);
字符串bestUrl=words.substring(startIndex,finishIndex);
System.out.println(最佳URL);
}
words=xy.readLine();
}
xy.close();
}捕获(ioe异常ioe){
System.out.println(ioe.getMessage());
}
}
}
任何想法都将不胜感激,非常感谢!!您将数组设置为越界,因为您在设置insons=true后忘记读取下一行。我在打印歌曲列表的代码块中添加了一个额外的读取行以及一个空检查。当我使用
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());
}
}
}
import java.io.*;
导入java.net。*;
公共类计数{
公共静态void main(字符串参数[]){
试一试{
字符串艺术家=args[0];
字符串优先=艺术家子字符串(0,1);
布尔内森=假;
字符串beginIndex=“h:\”;
字符串endIndex=“\”,c:;
int-one=1;
URL目录=新URL(“http://www.azlyrics.com/“+first+”/“+artist+”.html”);
URLConnection xx=discography.openConnection();
BufferedReader xy=新的BufferedReader(新的InputStreamReader(
xx.getInputStream());
字符串字=xy.readLine();
while(单词!=null){
if(words.equals(“var songlist=[”)){
内生=真;
words=xy.readLine();
}
if(words.equals(“var res=”
“;”){
内生=假;
打破
}
if(insons==true&&words!=null){
System.out.println(字);
int startIndex=words.indexOf(beginIndex,一);
系统输出打印项次(startIndex+6);
int finishIndex=words.indexOf(endIndex,一);
系统输出打印LN(finishIndex);
字符串bestUrl=words.substring(startIndex,finishIndex);
System.out.println(最佳URL);
}
words=xy.readLine();
}
xy.close();
}捕获(ioe异常ioe){
System.out.println(ioe.getMessage());
}
}
}
I thin在主逻辑{String bestUrl=words.substring(startIndex,finishIndex);}中,您需要检查startIndex和finishIndex是否不等于-1。这些正则表达式String beginIndex=“h:\”。“String endIndex=“\”,c:;
。找不到它们,这使得word.indexOf()=-1;
您能给我们展示一些示例输入和预期输出吗?一些边缘情况会很好。当我在没有导致错误的代码行的情况下运行它时,输出是红色的html行,字符串beginIndex出现的字符数,以及字符串endIndex出现的字符数。