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

Some web pages have color backgrounds. Some web pages use images Grab the color

ID: 3622103 • Letter: S

Question

Some web pages have color backgrounds. Some web pages use images Grab the color of the background if it has a color, otherwise get the name of the image Here is an example of a black background color <body text = “#FFFFFF” bgcolor = “#000000”> return 000000 in this example Here is an example of a background with an image <body background = “Computer-Background.jpg”> return Computer-Background.jpg in this example If the page has neither, then return “no background specified” return no background specified in this example

Use this method below to enter a url and return the page source, make sure to use substring



public String openURL(String url) throws Exception {   URL server = null;   try {   server = new URL(url); //connect to the target URL   } catch (MalformedURLException m) {   throw new Exception(m);   }
   BufferedReader in = null;   try {   in = new BufferedReader(new InputStreamReader(server.openStream())); //open a new stream buffer   } catch (IOException i) {   throw new Exception(i);   }
   String pageSource = "", SourceLine;   try {   while ((SourceLine = in.readLine()) != null) { //keep building the string until there is no more to read   pageSource += SourceLine + " ";   }
   } catch (IOException i) {   throw new Exception(i);   }
   try {   in.close(); //close the connection to the server   } catch (IOException i) {   throw new Exception(i);   }   return pageSource;   }

Explanation / Answer

let me know if this works for you. i can't really test it on google or yahoo since they dont use the exact keywords specified above. if the site has those exact keywords (bgcolor = "#123456") then the program works just fine. the way i tested it was i just created a simple .html file with some text and the exact phrase or the other one. heres the code. import java.io.*; import java.net.*; public class GetBackground { public static void main(String[] args) throws Exception { GetBackground back = new GetBackground(); int i = 0, j = 0; //Need to specify this String url = "http://"; String source = back.openURL(url); String color = ""; i = source.indexOf("bgcolor = "); if(i == -1) { i = source.indexOf("background = "); if(i == -1) { System.out.println("No background specified"); } else { i += "background = ".length()+1; j = source.indexOf(">",i)-1; color = source.substring(i,j); System.out.println("The background is: " + color); } } else { i += "bgcolor = ".length() + 2; color = source.substring(i,i+6); System.out.println("The background is: " + color); } } public String openURL(String url) throws Exception { URL server = null; try { server = new URL(url); } catch(MalformedURLException m) { throw new Exception(m); } BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(server.openStream())); } catch(IOException i) { throw new Exception(i); } String pageSource = "", SourceLine; try { while((SourceLine = in.readLine()) != null) { pageSource += SourceLine + " "; } } catch(IOException i) { throw new Exception(i); } try { in.close(); } catch(IOException i) { throw new Exception(i); } return pageSource; } }
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