Java URL解析程序不会停止循环
要求:Java URL解析程序不会停止循环,java,Java,要求: 我需要接受字符串作为参数 打印出它找到的第一个链接 返回一个整数,该整数是链接结束的索引,如果是,则返回-1 找不到任何链接 if语句如果在字符串中找不到“href”,它将返回整数,if语句在substring方法之前返回-1,它将防止获取索引越界错误 显示器需要返回字符串中的所有http站点 我相信我非常接近,以下是我的代码: public class LinkList { public static void main(String[] args) { String htm
public class LinkList {
public static void main(String[] args) {
String htmlCode = (a long string with all my url links)
int linkStart = 0;
int endLink = 0;
while (true) {
linkStart = hrefSearch(htmlCode);
if(linkStart == -1) break;
htmlCode = htmlCode.substring(linkStart);
}
}
public static int hrefSearch (String inStr) {
String find = " href=";
String closingBracket = ">";
int index = inStr.indexOf(find);
if (index != -1){
int closing = inStr.indexOf(closingBracket, index);
if (closing != -1){
System.out.println(inStr.substring (index, closing + 1));
}
}
return index;
}}
如何让程序停止循环并显示字符串中的所有URL?更改:
htmlCode = htmlCode.substring(linkStart);
致:
否则,您将一次又一次地查找第一个URL(因为
linkStart
是上一个URL的起始位置,所以htmlCode.substring(linkStart)
仍然包含找到的上一个URL)。您应该从阅读帮助中心开始。在你的问题正文和标题中有很多杂音,但没有实际的问题。你的代码产生了什么输出?什么不起作用?难道不是更合适吗?“href=”
?它不断循环,只显示其中一个URL,应该更多地显示在字符串中。我整天都在为这么小的事情用头敲击键盘。我将发布另一个我正在使用的程序的问题。你能帮我一下吗?@jc如果我看到它并能回答它,我可能会(假设其他人没有先回答)。顺便说一句,如果解决了你的问题,请接受答案。我如何接受答案,我是这个网站的新手。@jc欢迎来到StackOverflow!最好查看页面右上角的帮助链接。您可以通过单击答案旁边的复选标记来接受答案。
htmlCode = htmlCode.substring(linkStart+1);