Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This Exercise uses the String methods indexOf(), subString(), and replace(). It

ID: 3726945 • Letter: T

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 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<String> linkList = new ArrayList();
String htmlCode = "";
// This code will grab a webpage and store it in a string object.
htmlCode = new Scanner(new URL("http://web.nmsu.edu/~pbraker").openStream(), "UTF-8").useDelimiter("\A").next();

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<String> linkList, String inStr) {

//***** enter the method code here *********

}// end hrefSearch method
} // end WebCrawler

Explanation / Answer

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 linkList = new ArrayList(); String htmlCode = ""; // This code will grab a webpage and store it in a string object. htmlCode = new Scanner(new URL("http://web.nmsu.edu/~pbraker").openStream(), "UTF-8").useDelimiter("\A").next(); 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 linkList, String inStr) { int count = 0; int linkStart = 0, linkEnd; String htmlLink; while (true) { linkStart = inStr.indexOf("href=", linkStart); if(linkStart < 0) { break; } linkEnd = inStr.indexOf(">", linkStart); htmlLink = inStr.substring(linkStart+5, linkEnd); linkList.add(htmlLink); linkStart += htmlLink.length()+5; ++count; } return count; }// end hrefSearch method } // end WebCrawler
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote