2

So im trying to make a program that you input a flash game URL and it downloads the .swf file. Shown here:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


/**
 * Main.java
 *
 * 
 */
public class Main {

    /**
     * Reads a web page into a StringBuilder object
     * and prints it out to console along with the
     * size of the page.
     */
    public void getWebSite() {

        try {

            URL url = new URL("http://www.vivalagames.com");
            URLConnection urlc = url.openConnection();

            BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());

            StringBuilder builder = new StringBuilder();
            int byteRead;
            while ((byteRead = buffer.read()) != -1)
                builder.append((char) byteRead);

            buffer.close();

            Logger.log(builder.toString());
            System.out.println("The size of the web page is " + builder.length() + " bytes.");

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Starts the program
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().getWebSite();
    }
}

I have got to the part where it downloads the websites html and puts it into a file called output.txt. Now what im trying to do is make it search that text file till it finds the words ".swf", the searcher code is:

import java.io.*;
import java.util.Scanner;
import java.util.regex.MatchResult;

public class Sercher {
    public static void main() throws FileNotFoundException {
        Scanner s = new Scanner(new File("output.txt"));
        while (null != s.findWithinHorizon("(?i)\\b.swf\\b", 0)) {
            MatchResult mr = s.match();
            System.out.printf("Word found: %s at index %d to %d.%n", mr.group(),
                    mr.start(), mr.end());
        }


    }
}

Now how do I make the main.java code run the function from the Searcher.java?

4 Answers 4

2

This should do it:

public static void main(String[] args) {
    new Main().getWebSite();
    Searcher.main();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Make an instance of the Searcher class in the Main class.

public static void main(String[] args) {
   new Main().getWebSite();
   Searcher search = new Searcher();
}

or simply, use Searcher.main();.

Comments

0

First of all, storing the downloaded HTML in a file to re-read this file right after is not a really good idea. You could do everything in memory.

Think in terms of objects and methods. You basically have two objects here: a Downloader and a Searcher. And you don't want two main methods to your program: only a single one. This main method should look like this:

// create the object which downloads the HTML
Downloader downloader = new Downloader();

// Ask it to download, and store the result into a String variable
String downloadedHtml = downloader.download();

// create the object which can search into a String for .swf references
Searcher searcher = new Searcher();

// pass it the String to search into
searcher.searchSwfIn(downloadedHtml);

Comments

0

You need to put your classes to packages and import Searcher package to your main class. Example:

package foo.bar.package;    
import for.bar.package2.Searcher;
/* 
  Other import declarations
*/    
public class Main {    
/*
  Your code
*/     
    public static void main(String[] args) {
        new Main().getWebSite();
        new Searcher().search();    
    }    
}

package for.bar.package2;
/* 
  Import declarations
*/    
public class Searcher {
   public void search() throws FileNotFoundException {
        Scanner s = new Scanner(new File("output.txt"));
        while (null != s.findWithinHorizon("(?i)\\bjava\\b", 0)) {
            MatchResult mr = s.match();       

        System.out.printf("Word found: %s at index %d to %d.%n", mr.group(),
                mr.start(), mr.end());
    }

}

}

1 Comment

If both classed in the same package you don't need to import.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.