Question: This Exercise uses the String methods indexOf(), subString(), and replace(). It also uses an ArrayList as a container for the web links in a web
This Exercise uses the String methods indexOf(), subString(), and replace(). It also uses an ArrayList as a container for the web links in a web page. This Exercise builds on the LinkList Exercise except instead of printing the Link out to the console in the hrefSearch method; .add the link to an ArrayList after the surrounding hyphens have been removed. The main method has already been completed. Change the hrefSearch method to: 1. Remove the quotes from the string with the String .replace() method 2. Add the links to the ArrayList "linkList" When passing arguments to a method, primitive data types are passed by value. Objects are passed by reference. This means that an ArrayList declared in the main method can have values added to it in called methods by passing the reference to the ArrayList to the called method. Page 288 and 289 in the book explain this principle. The additional "for" loop in the Main Method prints out the links Output should look similar to: http://www.google.com http://web.nmsu.edu/~pbraker/ http://www.dogpile.com http://www.yahoo.com http://learn.nmsu.edu
What I have so far:
import java.io.IOException; import java.net.*; import java.util.*;
/** * Date Created * @author Student Name * FileName: WebCrawler.java */ public class WebCrawler {
/** * @param args the command line arguments */ public static void main(String[] args) throws MalformedURLException, IOException { ArrayList
int linkStart = 0; int endLink = 0; while (true) { linkStart = hrefSearch(linkList, htmlCode); if (linkStart == -1) { break; } htmlCode = htmlCode.substring(linkStart);
} // end while
for (String link : linkList) { System.out.println(link); } } // end Main method
public static int hrefSearch(ArrayList
}// end hrefSearch method } // end WebCrawler
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
