views:

698

answers:

6

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

+4  A: 

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.

GaryF
A: 

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

Read MS Excel files with Java

Chalkey
+3  A: 

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?

duffymo
because thats how you obtain the previosu results off of the official website, in an excel file
That might be how you download them from the site, but they don't necessarily have to stay there. It's your choice in the end, of course.
duffymo
ok then so i could save the file as a long list of numbers somehow? <how could i do this> then i could loop through and then no that every 6 numbers is a single draw
tom, not sure I understood, but if you're asking how to save a text file containing numbers, I suggest you check out FileUtils class in a library called Apache Commons IO - it'll make reading and writing files easier: http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html. SO also contains lots of related info; if you don't find what you need, post a question about that specific problem
Jonik
+4  A: 

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.

Duck
alternatively, one could ask why use Java? although there are many tools out there to link the two, there are also other languages that might be better suited to working with Excel than Java.
akf
@akf, good point; the question isn't very clear about this, but perhaps indeed the main point was to "have some fun and play around" reading Excel files. Heh :P
Jonik
@Jonik I guess it is a bit ambiguous but the title is "Making a lottery program in Java". That lead me to think his data, not his focus, was in Excel.
Duck
Yeah, that's how I interpreted it too, at first (and voted this up because what you say makes sense). In any case, the comments in http://stackoverflow.com/questions/1053131/making-a-lottery-program-in-java/1053156#1053156 reveal what's really going on
Jonik
@Duck,@Jonik: I agree, it was somewhat ambiguous. I was just trying to point out that Excel + Java might not be the best combination (as i think you alluded), and that the question could be looked at in two directions. the old 'right tool for the right task' conversation. but then again, this is a personal project, so the right tool is whatever he wants it to be, i guess.
akf
A: 

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.

kd304
A: 

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

Thorbjørn Ravn Andersen
And Excel will save into CSV format without difficulty. CSV files can be opened in a normal text editor, and can be parsed as a string.
TRiG