user2434615
user2434615

Reputation: 41

How to run a function from another .java file

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?

Upvotes: 2

Views: 4676

Answers (4)

DraggonZ
DraggonZ

Reputation: 1087

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());
    }

}

}

Upvotes: 0

Rhys
Rhys

Reputation: 2173

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();.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691635

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);

Upvotes: 0

Jules
Jules

Reputation: 15199

This should do it:

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

Upvotes: 2

Related Questions