import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WebHelper {
    public static String getWebContents(String url)
    {
        BufferedReader br = null;
        String result = null;
        try {
            URL toFetch = new URL(url);
            br = new BufferedReader(new InputStreamReader(toFetch.openStream()));
            StringBuilder ret = new StringBuilder();
            String line = br.readLine();
            while (line != null) {
                ret.append(line);
                line = br.readLine();
            }
            result = ret.toString();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            System.err.println("Malformed url:  " + url);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.err.println("Error reading from url:  " + url);
        } finally {
            if(br!=null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                    System.err.println("Couldn't close connection to url properly:  " + url);
                }
            }
            return result;
        }
    }

    public static void saveDoc(String contents, String filename)
    {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new FileWriter(new File(filename)));
            pw.print(contents);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.err.println("Error writing to file:  " + filename);
        } finally {
            if(pw!=null) {
                pw.close();
            }
        }
    }
    
    [B]public static int countMatches(String webpage, String keyword)[/B]
    {
            int count = 0;
    String textField = stripHTMLTags(webpage);
    int startIndex = 0;
    while(true)
        {
        int ind = textField.indexOf(keyword, startIndex);
        if (ind != -1){
            count++;
            startIndex = ind + 1;
            }
             else {
        break;
        }
        return count;
    }
    }
    
   
    public static String stripHTMLTags(String webpage)
    {

            String textField = webpage;

            int tagBegin= -1;
            int tagEnd= -1;
            while(true) {
                tagBegin = webpage.indexOf('<', tagBegin + 1);
                tagEnd = webpage.indexOf('>', tagEnd + 1);
                if(tagBegin == -1 || tagEnd == -1)
                    break;
                else {
                    {
                        textField= textField.replace(webpage.substring(tagBegin, tagEnd + 1), "");

                    }
                }
                    return textField;        
            }
 
    }

     public static String addFormat(String webpage, String toMatch, String format)
         {

         String testURL = "http://knight.cis.temple.edu/~yates/cis1068/au10/index.html";
            String contents = getWebContents(testURL);

            String newWebpage;
        if(format.equalsIgnoreCase("bold")) {
           contents = contents.replaceAll(toMatch, "<b>".concat(toMatch).concat("</b>"));}
        else if (format.equalsIgnoreCase("italics")) {
            newWebpage = contents.replaceAll(toMatch, "<i>".concat(toMatch).concat("</i>")); }
        else if (format.equalsIgnoreCase("underline")) {
            newWebpage = contents.replaceAll(toMatch, "<u>".concat(toMatch).concat("</u"));
                    }

               String saveFile = "test1.html";
                saveDoc(contents, saveFile);
         }

         public static String reverse(String webpage)
                 
                    {
                String reverseString = "";
                for(int i = webpage.length() - 1; i>=0; i--)
                    {
                    reverseString += webpage.charAt(i);

                    return reverseString;
                        }
                    }

         
       public static void main(String [] args) {
    String testURL = "http://knight.cis.temple.edu/~yates/cis1068/au10/index.html";
    String contents = getWebContents(testURL);
    String saveFile = "test.html";
        saveDoc(contents, saveFile);

       contents = contents.replaceAll("\\<.*?>","");
        contents = "<html><body>" + contents + "</body></html>";

        saveFile = "test1.html";
        saveDoc(contents, saveFile);
    
    
    }

These are the stipulations to my program:

  • Get a webpage: the program should prompt the user to enter a URL, and then download the html file at that URL and store it in a String.
  • Strip HTML tags: the program should remove all HTML "tags" from the String containing the html file. HTML tags are short sequences of characters that tell a browser how to format a Web page. All HTML tags start with the character '<' and end with the character '>', with any number of characters in between (e.g., "<a href=...>some text</a>"). The program should print the String after all HTML tags have been removed Return a String that strips away these tags, leaving just the text in between (e.g., "some text").
  • Add the tags "<html><body>" to the beginning of the String, and "</body></html>" to the end. This will allow a Web browser to interpret the String as a proper html file.
  • Count matches: the program should prompt the user to enter a String, and then print how many times that String appears on the webpage.
  • Bold: the program should prompt the user to enter a String str, and then format the Web document to put all appearances of str on the page in bold. To do this, surround every occurrence of str with "<b>" on the left and "</b>" on the right.
  • Italics: the program should prompt the user to enter a String str, and then format the Web document to put all appearances of str on the page in italics. To do this, surround every occurrence of str with "<i>" on the left and "</i>" on the right.
  • Underline: the program should prompt the user to enter a String str, and then format the Web document to underline all appearances of str on the page. To do this, surround every occurrence of str with "<u>" on the left and "</u>" on the right.
  • Reverse: the program should prompt the user for a start position and an end position, and then reverse the order of the characters between those positions in the String.
  • Save: Finally, the program should save the results to a ".html" file.

I've gotten most of the stipulations down in my coding, but I can't run the program because there are numerous 'missing return statement' errors that I've been racking my brain over. I'm also not even sure if any of the coding following the bolded print is correct due to the fact that I cannot run the program. I'm not yet finished, as I still have to tie up the program.

Any insight would be helpful. I've been working on this for hours and to no avail.

There are places you need to change in your code because you seem to excessively use "while(true)" and that is not a good way of doing in this case unless you are doing concurrent programming...

// first one
public static int countMatches(String webpage, String keyword) {
  int count = 0;
  String textField = stripHTMLTags(webpage);
  int startIndex = 0;
  while(true) {
    int ind = textField.indexOf(keyword, startIndex);  // should declare ind outside
    if (ind != -1){
      count++;
      startIndex = ind + 1;
    }
    else {
      break;  // break without return!
    }
    return count;  // if it didn't break, it would return the integer!
  }
  // now if it executes the "break" command, the method doesn't return anything... ERROR!
}

// 2nd one...
public static String stripHTMLTags(String webpage) {
  String textField = webpage;
  int tagBegin= -1;
  int tagEnd= -1;
  while(true) {  // again!
    tagBegin = webpage.indexOf('<', tagBegin + 1);
    tagEnd = webpage.indexOf('>', tagEnd + 1);
    if(tagBegin == -1 || tagEnd == -1)
      break;  // again! If break, your method will not return anything!
    else {
      {  // double open curly bracket???
      textField= textField.replace(webpage.substring(tagBegin, tagEnd + 1), "");
      }
    }
    return textField;        
  }
  // same problem... If it breaks from the "if" statement, it gets ERROR.
}

// 3rd one
public static String addFormat(String webpage, String toMatch, String format) {
  String testURL = "http://knight.cis.temple.edu/~yates/cis1068/au10/index.html";
  String contents = getWebContents(testURL);
  String newWebpage;
  if(format.equalsIgnoreCase("bold")) {
    contents = contents.replaceAll(toMatch, "<b>".concat(toMatch).concat("</b>"));
  }
  else if (format.equalsIgnoreCase("italics")) {
    newWebpage = contents.replaceAll(toMatch, "<i>".concat(toMatch).concat("</i>"));
  }
  else if (format.equalsIgnoreCase("underline")) {
    newWebpage = contents.replaceAll(toMatch, "<u>".concat(toMatch).concat("</u"));
  }
  String saveFile = "test1.html";
  saveDoc(contents, saveFile);
  // where is the return statement here? You declare that the method will return String
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.