简体   繁体   中英

Making a lottery program in Java

I was wondering, what do i need to look at to get cracking with making a programme that can read from a excel file

I was thinking of making a programme that uses a excel spreadsheet and reading them in and generating a list of combinations that have not occured yet

Its just for a bit of fun, but could be a good challenege

thanks

The direct route to getting Excel data into Java is, of course, POI . Very stable, excellent library, and lets you in and the low-level innards of working with Excel.

Of note is the Busy Developer's Guide to POI , which should help ease some of the initial pain.

If you're more interested in learning POI and doing a simple Java exercise, then this sounds fair enough. The more interesting questions will be how to display the millions of combinations that haven't already occurred, and how to approach this from a data structure point of view (hint: use a mix of hashtable lookups and generation to keep the memory overhead to a minimum).

If you want to take this really seriously, ask yourself if an Excel file is a good storage mechanism for this kind of data. That's really what you're doing: using Excel as a data store. There are better alternatives.

Andy Khan's JExcel is the way to go if you must use Excel. I've found it to be far superior to POI.

Personally I don't see what it's buying you here. You could generate all the combinations in a flat file or use a real database if its required. What's the draw with Excel besides familiarity and ubiquity?

Why go the Excel route at all?

If the point is to have some fun and play around in Java, Excel must have some way to produce a comma delimited file from the data. Just read that and do whatever it is you want to do.

Here is an article that might help you along...

Read MS Excel files with Java

As mentioned by others, there are several libraries and several posts available for you to get started. I just wanted to give you two approaches to achieve your goal.

For the case of 'generating a list of combinations that have not occured yet'

I understand this goal as you have a history of the draws and you want to generate a bunch of random number sets which have never occurred in the history. Basically you need first a random number generator and generate the lottery numbers. Then take this set of numbers and try to find it in the historical data. If you found it, just start over and generate a new one. Based on my comment to the question you practically have the chance lets say (5200 / 47M) you find your numbers in the history list. Repeat this whole process until you get enough number.

For the case of 'generating a list of combinations that have occured already'

You basically need to find duplicates in the history list. Create a Set<Integer> from each historical occurrence and start adding it to yet another set of Set<Set<Integer>> . If the number set you are adding already occurred during the processing, you get a false return from the add method of this latter set. Then you just print or memorize the duplicate number set.

Use comma separated files instead of native Excel files. Will make your life MUCH easier :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM