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

I need help with this Java program. The instructions are: Write a program to que

ID: 3715425 • Letter: I

Question

I need help with this Java program.

The instructions are:

Write a program to query

http://www.pravdareport.com

http://pravda.ru

http://www.lefigaro.fr

http://www.independent.co.uk

and display everything between the first occurrence of the tags

<title> and </title>

Things like <whatever> are called HTML tags. The </title> ends the tag <title>.

If a webpage contains


<title>Sophie, Sally and Jack</title>


your program should print

Sophie, Sally and Jack

If no <title> … </title> tags appear, print a message such as “No <title> was found in http://pravda.ru.”

Your program should handle any thrown exceptions with an informative message.

Grading Elements:

Submission adheres to your instructor’s rules for submissions and code quality and conventions.

Program opens network connections to each of the designated web sites

Program reads the designated web pages

Program scans the incoming datastream for the appropriate HTML tags

Program extracts the designated information, or prints a message if no such information is found

Program catches any exceptions and prints an informative

?*TrthTest.java ? DHtmlReader.java 1e import java.io.IOException; 2 import java.net.MalformedURLException; 3 import java.net.URL; 4 import java.util.Scanner 6 public class TruthTest { 8 public static void main(String[] args) throws MalformedURLExceptionf 10 Scanner scan null; 12 13 try t 15 116 String out - new Scanner (new URL"http:/lpravda.u).openStream).useDelimiter(A").next); string title = out, substring(out.index0f("") + 7, out .index0f("

Explanation / Answer

import java.net.*;

import java.io.*;

public class TitleTest {

public static void main(String[] args) throws IOException {

   // Make an array of all the URLs to make it more dynamic

   // our array declaration

   String[] URLArray = new String[]{"http://www.pravdareport.com",

           "http://pravda.ru",

           "http://www.lefigaro.fr",

           "http://www.independent.co.uk"};

      

   for(int i = 0; i < URLArray.length; i++) {

       Boolean b = new Boolean(false);

       try{

             // Make all the URLs to the web pages

             URL url = new URL(URLArray[i]);

          

             // Get the input stream through URL Connection

      URLConnection con= url.openConnection();

      InputStream inputStream = con.getInputStream();

      BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

      String line = null;

  

      // read each line and check if title tag is there

      while ((line = br.readLine()) != null) {

         if(line.indexOf("<title>") != -1){

             b = true;

             line = line.substring(line.indexOf("<title>")+7, line.indexOf("</title>"));

             System.out.println(URLArray[i] + " title- '" + line + "'");

         }

      }

      //if no title tag is found

   if(b == false){

       System.out.println("Title tag could not be found for " + URLArray[i]);

   }

       } catch(Exception e){

           e.printStackTrace();

           System.out.print(e);

       }

   }

}

}