Home > java > Java Powerball Project

Java Powerball Project

Everybody know about the Powerball Lotto program. I am going to combine the fun of Java with the past Powerball winning numbers to come up with some basic statistics. I am by no means making claim to be able to predict winning Powerball Numbers. That’s just crazy. This is just for fun!

This Java program is a work in progress and this page will be updated as different statistical methods are added. Let’s start with a few basics. You pick five numbers that range between 1 and 69. You also pick red Powerball number that is between 1 and 26, ending up with six numbers for your Powerball number set.

  1. Phase 1 – Define Static Variables
  2. Phase 2 – Get Past Winning Numbers
  3. Phase 3 – Store Past Winning Numbers
  4. Phase 4 – Determine Individual Number Frequency (TBD)
  5. Phase 5 – Determine Red Powerball Number Frequency (TBD)
  6. Phase 6 – Determine Number Pairs Frequency (TBD)


Java Powerball Static Variables

Let’s first define some static variables that we need. We have the low and high number range for the first five numbers. Follow that with the low and high range for the Powerball number. The last two Java parms are the URL to get the past drawings list and the location to store that on my laptop.


//First five numbers lower/upper range
private static final int lowNumber = 1;
private static final int highNumber = 69;
	
//Powerball number lower/upper range
private static final int lowPBNumber = 1;
private static final int highPBNumber = 26;

//CSV file of past Powerball drawings
private static String pastDrawsFile = "C:\\Users\\Admin\\Documents\\Workspace\\powerball\\pastdraws\\winnums-text.txt";
	
//URL to Powerball past drawings
private static String powerBallWinnersNumbersURL = "http://www.powerball.com/powerball/winnums-text.txt";


Java Powerball Get Past Winning Numbers

My next task is to create a method that will go out and get the Powerball past winnings file and store it locally to my laptop. This method

public static void getPastWinningsFile(String powerBallWinnersNumbersURL, String pastDrawsFile)
	{
		StringBuffer stringBuffer = new StringBuffer();		
		
		try 
		{
			URL url = new URL(powerBallWinnersNumbersURL);
			InputStreamReader inputStreamReader = new InputStreamReader(url.openStream(), "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			
			String contentLine = bufferedReader.readLine();
			   
			while (contentLine != null) 
			{
			      contentLine = bufferedReader.readLine();	
			      stringBuffer.append("\n"+contentLine);
			}
			
			//Close BufferedReader
			bufferedReader.close();
			
			//Write the contents of file URL to local file
			FileWriter fileWriter = new FileWriter(pastDrawsFile, false);
			
			//Write the data
			fileWriter.write(stringBuffer.toString());

			//Close the handle
			fileWriter.close();
			
		}
		catch(Exception ex)
		{
			System.out.println("Exception:"+ex.toString());
		}
		
	}


Java Powerball Store Past Winning Numbers

public static void storePastWinningsNumbers()
	{
		
		String eachLine = "";
		String lastDate = "";
        ArrayList<String> numberSets = new ArrayList<String>();
		ArrayList<Integer> pbNumbers = new ArrayList<Integer>();

		
		try 
		{
			BufferedReader buffReader = new BufferedReader(new FileReader(pastDrawsFile));
		
	    	while ((eachLine = buffReader.readLine()) != null)
	    	{
	    	
	    		//Ignore all the lines that are not number sets
	    		if(eachLine.length() < 10) { 
	    			continue;
	    		}
	    	
	    		//Split the line by space
	    		String splitLine[] = eachLine.split("  ");
	    	
	    		//Add all six numbers to ArrayList
	    		numberSets.add(splitLine[1]+" "+splitLine[2]+" "+splitLine[3]+" "+splitLine[4]+" "+splitLine[5]+" "+splitLine[6]);
	    	
	    		//Add just the sixth number (Powerball) to an ArrayList
	    		pbNumbers.add(Integer.parseInt(splitLine[6]));
	    	
	    		//Capture the date of the earliest winning number set
	    		lastDate = splitLine[0];
	    	}
	    
	    	//Set the earliest date of drawing
	    	earliestDate = lastDate;
	    
	    	//Store the number of sets
	    	sampleSize = numberSets.size();
	    
	    	//Close BufferedReader object
	    	buffReader.close();
		}
		catch(Exception ex)
		{
			System.out.println("Exception:"+ex.toString());
		}
	}


Java Powerball Individual Number Frequency

Java method to determine which numbers (first five) are drawn the most often. This Java method will be coming soon.


Java Powerball Red Powerball Number Frequency

Java method to determine which of the Red Powerball number (sixth number) is drawn most often. This Java method will be coming soon.


Java Powerball Number Pairs Frequency

Java method to determine which pairs of numbers are most often drawn together. This Java method will be coming soon.



Java References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

(1) Comment

  1. Kevin

    I sure could use the winning numbers. Just email me them before the next drawing!

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot