
import java.awt.Font;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Bill Kraynek
 */
public class Lotto {

    static class LottoCount implements Comparable<LottoCount> {

        int number;
        int count;

        public LottoCount(int number, int count) {
            this.number = number;
            this.count = count;
        }

        public int compareTo(LottoCount that) {
            return that.count - this.count;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        String address = "http://www.flalottery.com/exptkt/l6.htm";
        URL locator = new URL(address);
        Scanner fileScanner = new Scanner(locator.openStream());
        int[] lottoNums = new int[53];
        for (int i = 0; i < 53; i++) {
            lottoNums[i] = 0;
        }
        ArrayList<Integer> lottos = new ArrayList<Integer>();
        ArrayList<LottoCount> counts = new ArrayList<LottoCount>();
        for (int i = 0; i < 53; i++) {
            lottos.add(0);
            counts.add(new LottoCount(i, 0));
        }// end for
        String out = "";
        while (fileScanner.hasNext()) {
            fileScanner.useDelimiter("[>][1-9][0-9]*[<]");
            fileScanner.next();
            fileScanner.useDelimiter("</font");
            if (fileScanner.hasNext()) {
                String save = fileScanner.next();
                if (!save.contains("/b")) {
                    int index = new Integer(save.substring(1)) - 1;
                    lottoNums[index]++;
                    lottos.set(index, lottos.get(index) + 1);
                    counts.get(index).count++;
                }// end if
            }//end if
        }// end while
        String numsOut = "";
        for (int i = 0; i < 53; i++) {
            numsOut += (i + 1) + " " + lottoNums[i] + " " + lottos.get(i) + " " + counts.get(i).count + "\n";
        }// end for
        JTextArea outArea = new JTextArea(numsOut, 30, 30);
        JOptionPane.showMessageDialog(null, new JScrollPane(outArea));
        numsOut = "Frequency of Florida Lotto Numbers\n";

        Collections.sort(counts);
        for (LottoCount x : counts) {
            numsOut += String.format("%6d appeared%4d times\n", (x.number + 1), x.count);
        }// end for
        outArea = new JTextArea(numsOut, 50, 35);

        outArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
        JOptionPane.showMessageDialog(null, new JScrollPane(outArea));
    }
}

