Speed up Java program faster

4206942069 Regular
edited October 2010 in Tech & Games
No coding forums so if anyone knows about java;
I just need a few simple alterations or additions to this program
it reads web pages then saves them in files and creates directories for the files

just does it kind of slowly
package URLExp;
import java.net.URL;
import java.net.URLConnection;
import java.io.*;
import java.util.Scanner;


public class Main {


	public static void main(String[] args) {

            Scanner keyboard = new Scanner(System.in);

            int count = 1, end = 1;
       

            System.out.println("Enter Starting Number");
            count = keyboard.nextInt();

            System.out.println("Enter Ending Number");
            end = keyboard.nextInt();


            
            try {

                while(count <= end)

                {
			URL plants = new URL("http://davesgarden.com/guides/pf/print.php?pid=" +count);

			URLConnection yc = plants.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc
					.getInputStream()));
			

                        //my code-------------------------------------
                        String input;
                        int check;

                        System.out.println("");
                        System.out.println("Plant #" +count);

                        while ((input = in.readLine()) != null)                        
                        {
                        // start it
                        check = getCommonName(input);

                        if (check >= 9)
                        {
                            System.out.println("Blank Page");
                            break;
                        }
                     
                        }                                                            
			in.close();

                        count++;
                        
                }
                       

		} catch (Exception e) {
			e.printStackTrace();
		}

            }

	
    public static int getCommonName(String inLine)
    {

    String commonName ="", input = inLine;
    int counterI, counterII, check;

                            check = 0;
                            counterI = 0;
                            counterII = 1;                            

                            while((counterII-counterI) < 7)
                            {
                                while ((input.charAt(counterI)) != '>')
                                counterI++;

                                counterII = counterI;
                                while ((input.charAt(counterII)) != '<')
                                counterII++;

                                check++;
                                if(check>3)
                                {
                                counterI++;
                                while ((input.charAt(counterI)) != '>')
                                counterI++;
                                
                                counterII = counterI;
                                while ((input.charAt(counterII)) != '<')
                                counterII++;

                                check++;
                                if(check>6)
                                {
                                counterI++;
                                while ((input.charAt(counterI)) != '>')
                                counterI++;

                                counterII = counterI;
                                while ((input.charAt(counterII)) != '<')
                                counterII++;

                                check++;
                                if(check>=9)
                                    {
                                    return(check);
                                    }
                                }
                                }
                            }                       

                       commonName = input.substring(counterI+1, counterII);
                       System.out.println(commonName);

                       check = getScientificName(input, commonName, counterI, counterII);

                       return(check);

        }

    public static int getScientificName(String inLine, String Cname,
            int counter1, int counter2)
    {
        String input = inLine, scientificName = "", commonName = Cname;
        int counterI = counter1, counterII = counter2, check;
        
                        check = 0;
                        counterI += 5;
                        counterII = counterI;
                        
                        while((counterII-counterI) < 7)
                            {
                                while ((input.charAt(counterI)) != '>')
                                counterI++;

                                counterII = counterI;
                                while ((input.charAt(counterII)) != '<')
                                counterII++;

                                check++;
                                if(check>3)
                                {
                                counterI++;
                                while ((input.charAt(counterI)) != '>')
                                counterI++;

                                counterII = counterI;
                                while ((input.charAt(counterII)) != '<')
                                counterII++;

                                if(check>6)
                                {
                                counterI++;
                                while ((input.charAt(counterI)) != '>')
                                counterI++;

                                counterII = counterI;
                                while ((input.charAt(counterII)) != '<')
                                counterII++;
                                
                                if(check>9)
                                    {
                                    System.out.println("Error Reading Scientific Name");
                                    return(check);
                                    }
                                }
                                }
                            }
                       scientificName = input.substring(counterI+1, counterII);
                       System.out.println(scientificName);

                       getFamily(input, commonName, scientificName);

                       return(check);
    }

    public static void getFamily(String inLine, String Cname, String Sname)
    {
        int counterIII, counterIV;
        String family = "", input = inLine, commonName = Cname, scientificName = Sname;
        
                        counterIII = 0;
                        while (input.charAt(counterIII) != ':')
                            counterIII++;

                        counterIV = counterIII;
                        while (input.charAt(counterIV) != '<')
                            counterIV++;
                        
                        family = input.substring(counterIII+2, counterIV);
                        System.out.println("Family: "+family);
                        
                        getGenus(input, commonName, scientificName, family, counterIII, counterIV);
                        
         
        
    }

    public static void getGenus(String inLine, String Cname, String Sname,
            String fam, int counter3, int counter4)
    {
        int counterIII = counter3, counterIV = counter4;
        String input = inLine, commonName = Cname, scientificName = Sname, genus = "";
        String family = fam;

                        counterIII++;
                        while (input.charAt(counterIII) != ':')
                            counterIII++;

                        counterIV = counterIII;
                        while (input.charAt(counterIV) != '/')
                            counterIV++;
                        genus = input.substring(counterIII+5, counterIV-1);

                        System.out.println("Genus: "+genus);

                        getSpecies(input, commonName, scientificName, family, genus, counterIII, counterIV);
    }

    public static void getSpecies(String inLine, String Cname, String Sname,
            String fam, String gen, int counter3, int counter4)
    {
        int counterIII = counter3, counterIV = counter4;
        String input = inLine, commonName = Cname, scientificName = Sname, species = "";
        String family = fam, genus = gen;

        String searchMe = input;
        String substring = "Species";
        boolean found = (searchMe.contains(substring) ? true : false);
        String searchMe2 = input;
        String substring2 = "Cultivar";
        boolean found2 = (searchMe2.contains(substring2) ? true : false);

        if(found == true)
        {
                        counterIII++;
                        while (input.charAt(counterIII) != ':')
                            counterIII++;

                        counterIV = counterIII;
                        while (input.charAt(counterIV) != '/')
                            counterIV++;
                        species = input.substring(counterIII+5, counterIV-1);

                        System.out.println("Species: "+species);
        }
        else if(found2 == true)
        {
                        counterIII++;
                        while (input.charAt(counterIII) != ':')
                            counterIII++;

                        counterIV = counterIII;
                        while (input.charAt(counterIV) != '/')
                            counterIV++;
                        species = input.substring(counterIII+2, counterIV-1);

                        scientificName = species;
                        System.out.println("Cultivar: "+species);

        }

                        saveData(input, scientificName, family, genus, species);
    }

    public static void saveData(String inLine, String Sname,
            String fam, String gen, String spec)
    {
        String input = inLine, scientificName = Sname, family = fam;
        String genus = gen, species = spec;
         String path="C:/Plant_Database"+"/"+family+"/"+genus+"/"+species;

    FileOutputStream out; // declare a file output object
                PrintStream data; // declare a print stream object

    try
    {


    // Create multiple directories
    boolean success = (new File(path)).mkdirs();
    if (success) {
      System.out.println("Directories: " + path + " created");
    }

    }
    catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }



        try
                {
                        // Create a new file output stream
                        // connected to "myfile.txt"
                        out = new FileOutputStream(path+"/"+scientificName+".php");
                        // Connect print stream to the output stream
                        data = new PrintStream( out );
                        data.println (input);
                        data.close();
                }
        catch (Exception e)
                {
                        System.err.println ("Error writing to file");
                }



    }
}
Sign In or Register to comment.